00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "kateviewinternal.h"
00027 #include "kateviewinternal.moc"
00028
00029 #include "kateview.h"
00030 #include "katecodefoldinghelpers.h"
00031 #include "kateviewhelpers.h"
00032 #include "katehighlight.h"
00033 #include "katesupercursor.h"
00034 #include "katerenderer.h"
00035 #include "katecodecompletion.h"
00036 #include "kateconfig.h"
00037
00038 #include <kcursor.h>
00039 #include <kdebug.h>
00040 #include <kapplication.h>
00041 #include <kglobalsettings.h>
00042 #include <kurldrag.h>
00043
00044 #include <qstyle.h>
00045 #include <qdragobject.h>
00046 #include <qpopupmenu.h>
00047 #include <qdropsite.h>
00048 #include <qpainter.h>
00049 #include <qlayout.h>
00050 #include <qclipboard.h>
00051 #include <qpixmap.h>
00052 #include <qvbox.h>
00053
00054 KateViewInternal::KateViewInternal(KateView *view, KateDocument *doc)
00055 : QWidget (view, "", Qt::WStaticContents | Qt::WRepaintNoErase | Qt::WResizeNoErase )
00056 , editSessionNumber (0)
00057 , editIsRunning (false)
00058 , m_view (view)
00059 , m_doc (doc)
00060 , cursor (doc, true, 0, 0, this)
00061 , possibleTripleClick (false)
00062 , m_dummy (0)
00063 , m_startPos(doc, true, 0,0)
00064 , m_madeVisible(false)
00065 , m_shiftKeyPressed (false)
00066 , m_autoCenterLines (false)
00067 , m_selChangedByUser (false)
00068 , selectAnchor (-1, -1)
00069 , m_selectionMode( Default )
00070 , m_preserveMaxX(false)
00071 , m_currentMaxX(0)
00072 , m_usePlainLines(false)
00073 , m_updatingView(true)
00074 , m_cachedMaxStartPos(-1, -1)
00075 , m_dragScrollTimer(this)
00076 , m_scrollTimer (this)
00077 , m_cursorTimer (this)
00078 , m_textHintTimer (this)
00079 , m_textHintEnabled(false)
00080 , m_textHintMouseX(-1)
00081 , m_textHintMouseY(-1)
00082 , m_imPreeditStartLine(0)
00083 , m_imPreeditStart(0)
00084 , m_imPreeditLength(0)
00085 , m_imPreeditSelStart(0)
00086 {
00087 setMinimumSize (0,0);
00088
00089
00090 cursor.setMoveOnInsert (true);
00091
00092
00093 selStartCached.setLine( -1 );
00094
00095
00096
00097 m_lineScroll = new KateScrollBar(QScrollBar::Vertical, this);
00098 m_lineScroll->show();
00099 m_lineScroll->setTracking (true);
00100
00101 m_lineLayout = new QVBoxLayout();
00102 m_colLayout = new QHBoxLayout();
00103
00104 m_colLayout->addWidget(m_lineScroll);
00105 m_lineLayout->addLayout(m_colLayout);
00106
00107
00108 m_dummy = new QWidget(m_view);
00109 m_dummy->setFixedHeight(style().scrollBarExtent().width());
00110
00111 if (m_view->dynWordWrap())
00112 m_dummy->hide();
00113 else
00114 m_dummy->show();
00115
00116 m_lineLayout->addWidget(m_dummy);
00117
00118
00119 connect(m_lineScroll, SIGNAL(prevPage()), SLOT(scrollPrevPage()));
00120 connect(m_lineScroll, SIGNAL(nextPage()), SLOT(scrollNextPage()));
00121
00122 connect(m_lineScroll, SIGNAL(prevLine()), SLOT(scrollPrevLine()));
00123 connect(m_lineScroll, SIGNAL(nextLine()), SLOT(scrollNextLine()));
00124
00125 connect(m_lineScroll, SIGNAL(sliderMoved(int)), SLOT(scrollLines(int)));
00126 connect(m_lineScroll, SIGNAL(sliderMMBMoved(int)), SLOT(scrollLines(int)));
00127
00128
00129 m_lineScroll->installEventFilter(this);
00130
00131
00132
00133
00134 m_columnScroll = new QScrollBar(QScrollBar::Horizontal,m_view);
00135
00136
00137 if (m_view->dynWordWrap())
00138 m_columnScroll->hide();
00139 else
00140 m_columnScroll->show();
00141
00142 m_columnScroll->setTracking(true);
00143 m_startX = 0;
00144
00145 connect( m_columnScroll, SIGNAL( valueChanged (int) ),
00146 this, SLOT( scrollColumns (int) ) );
00147
00148
00149
00150
00151 leftBorder = new KateIconBorder( this, m_view );
00152 leftBorder->show ();
00153
00154 connect( leftBorder, SIGNAL(toggleRegionVisibility(unsigned int)),
00155 m_doc->foldingTree(), SLOT(toggleRegionVisibility(unsigned int)));
00156
00157 connect( doc->foldingTree(), SIGNAL(regionVisibilityChangedAt(unsigned int)),
00158 this, SLOT(slotRegionVisibilityChangedAt(unsigned int)));
00159 connect( doc, SIGNAL(codeFoldingUpdated()),
00160 this, SLOT(slotCodeFoldingChanged()) );
00161
00162 displayCursor.setPos(0, 0);
00163 cursor.setPos(0, 0);
00164 cXPos = 0;
00165
00166 setAcceptDrops( true );
00167 setBackgroundMode( NoBackground );
00168
00169
00170 installEventFilter(this);
00171
00172
00173 setInputMethodEnabled(true);
00174
00175
00176 setCursor( KCursor::ibeamCursor() );
00177 m_mouseCursor = IbeamCursor;
00178
00179
00180 setMouseTracking(true);
00181
00182 dragInfo.state = diNone;
00183
00184
00185 connect( &m_dragScrollTimer, SIGNAL( timeout() ),
00186 this, SLOT( doDragScroll() ) );
00187
00188 connect( &m_scrollTimer, SIGNAL( timeout() ),
00189 this, SLOT( scrollTimeout() ) );
00190
00191 connect( &m_cursorTimer, SIGNAL( timeout() ),
00192 this, SLOT( cursorTimeout() ) );
00193
00194 connect( &m_textHintTimer, SIGNAL( timeout() ),
00195 this, SLOT( textHintTimeout() ) );
00196
00197
00198 connect( m_view, SIGNAL( selectionChanged() ),
00199 this, SLOT( viewSelectionChanged() ) );
00200
00201
00202
00203
00204
00205
00206 if (QApplication::reverseLayout()){
00207 m_view->m_grid->addMultiCellWidget(leftBorder, 0, 1, 2, 2);
00208 m_view->m_grid->addMultiCellWidget(m_columnScroll, 1, 1, 0, 1);
00209 m_view->m_grid->addMultiCellLayout(m_lineLayout, 0, 0, 0, 0);
00210 }
00211 else{
00212 m_view->m_grid->addMultiCellLayout(m_lineLayout, 0, 1, 2, 2);
00213 m_view->m_grid->addMultiCellWidget(m_columnScroll, 1, 1, 0, 1);
00214 m_view->m_grid->addWidget(leftBorder, 0, 0);
00215 }
00216
00217 updateView ();
00218 }
00219
00220 KateViewInternal::~KateViewInternal ()
00221 {
00222 }
00223
00224 void KateViewInternal::prepareForDynWrapChange()
00225 {
00226
00227 m_wrapChangeViewLine = displayViewLine(displayCursor, true);
00228 }
00229
00230 void KateViewInternal::dynWrapChanged()
00231 {
00232 if (m_view->dynWordWrap())
00233 {
00234 m_columnScroll->hide();
00235 m_dummy->hide ();
00236 }
00237 else
00238 {
00239 m_columnScroll->show();
00240 m_dummy->show ();
00241 }
00242
00243 tagAll();
00244 updateView();
00245
00246 if (m_view->dynWordWrap())
00247 scrollColumns(0);
00248
00249
00250 if (m_wrapChangeViewLine != -1) {
00251 KateTextCursor newStart = viewLineOffset(displayCursor, -m_wrapChangeViewLine);
00252 makeVisible(newStart, newStart.col(), true);
00253 } else {
00254 update();
00255 }
00256 }
00257
00258 KateTextCursor KateViewInternal::endPos() const
00259 {
00260 int viewLines = linesDisplayed() - 1;
00261
00262 if (viewLines < 0) {
00263 kdDebug(13030) << "WARNING: viewLines wrong!" << endl;
00264 viewLines = 0;
00265 }
00266
00267
00268 if (!lineRanges.count() || lineRanges[0].line == -1 || viewLines >= (int)lineRanges.count()) {
00269
00270 return KateTextCursor(m_doc->numVisLines() - 1, m_doc->lineLength(m_doc->getRealLine(m_doc->numVisLines() - 1)));
00271 }
00272
00273 for (int i = viewLines; i >= 0; i--) {
00274 KateLineRange& thisRange = lineRanges[i];
00275
00276 if (thisRange.line == -1) continue;
00277
00278 if (thisRange.virtualLine >= (int)m_doc->numVisLines()) {
00279
00280 return KateTextCursor(m_doc->numVisLines() - 1, m_doc->lineLength(m_doc->getRealLine(m_doc->numVisLines() - 1)));
00281 }
00282
00283 return KateTextCursor(thisRange.virtualLine, thisRange.wrap ? thisRange.endCol - 1 : thisRange.endCol);
00284 }
00285
00286 Q_ASSERT(false);
00287 kdDebug(13030) << "WARNING: could not find a lineRange at all" << endl;
00288 return KateTextCursor(-1, -1);
00289 }
00290
00291 uint KateViewInternal::endLine() const
00292 {
00293 return endPos().line();
00294 }
00295
00296 KateLineRange KateViewInternal::yToKateLineRange(uint y) const
00297 {
00298 uint range = y / m_view->renderer()->fontHeight();
00299
00300
00301 if (range >= lineRanges.size())
00302 return lineRanges[lineRanges.size()-1];
00303
00304 return lineRanges[range];
00305 }
00306
00307 int KateViewInternal::lineToY(uint viewLine) const
00308 {
00309 return (viewLine-startLine()) * m_view->renderer()->fontHeight();
00310 }
00311
00312 void KateViewInternal::slotIncFontSizes()
00313 {
00314 m_view->renderer()->increaseFontSizes();
00315 }
00316
00317 void KateViewInternal::slotDecFontSizes()
00318 {
00319 m_view->renderer()->decreaseFontSizes();
00320 }
00321
00325 void KateViewInternal::scrollLines ( int line )
00326 {
00327 KateTextCursor newPos(line, 0);
00328 scrollPos(newPos);
00329 }
00330
00331
00332 void KateViewInternal::scrollViewLines(int offset)
00333 {
00334 KateTextCursor c = viewLineOffset(startPos(), offset);
00335 scrollPos(c);
00336
00337 m_lineScroll->blockSignals(true);
00338 m_lineScroll->setValue(startLine());
00339 m_lineScroll->blockSignals(false);
00340 }
00341
00342 void KateViewInternal::scrollNextPage()
00343 {
00344 scrollViewLines(kMax( (int)linesDisplayed() - 1, 0 ));
00345 }
00346
00347 void KateViewInternal::scrollPrevPage()
00348 {
00349 scrollViewLines(-kMax( (int)linesDisplayed() - 1, 0 ));
00350 }
00351
00352 void KateViewInternal::scrollPrevLine()
00353 {
00354 scrollViewLines(-1);
00355 }
00356
00357 void KateViewInternal::scrollNextLine()
00358 {
00359 scrollViewLines(1);
00360 }
00361
00362 KateTextCursor KateViewInternal::maxStartPos(bool changed)
00363 {
00364 m_usePlainLines = true;
00365
00366 if (m_cachedMaxStartPos.line() == -1 || changed)
00367 {
00368 KateTextCursor end(m_doc->numVisLines() - 1, m_doc->lineLength(m_doc->getRealLine(m_doc->numVisLines() - 1)));
00369
00370 m_cachedMaxStartPos = viewLineOffset(end, -((int)linesDisplayed() - 1));
00371 }
00372
00373 m_usePlainLines = false;
00374
00375 return m_cachedMaxStartPos;
00376 }
00377
00378
00379 void KateViewInternal::scrollPos(KateTextCursor& c, bool force, bool calledExternally)
00380 {
00381 if (!force && ((!m_view->dynWordWrap() && c.line() == (int)startLine()) || c == startPos()))
00382 return;
00383
00384 if (c.line() < 0)
00385 c.setLine(0);
00386
00387 KateTextCursor limit = maxStartPos();
00388 if (c > limit) {
00389 c = limit;
00390
00391
00392 if (!force && ((!m_view->dynWordWrap() && c.line() == (int)startLine()) || c == startPos()))
00393 return;
00394 }
00395
00396 int viewLinesScrolled = 0;
00397
00398
00399
00400
00401 bool viewLinesScrolledUsable = !force
00402 && (c.line() >= (int)startLine()-(int)linesDisplayed()-1)
00403 && (c.line() <= (int)endLine()+(int)linesDisplayed()+1);
00404
00405 if (viewLinesScrolledUsable)
00406 viewLinesScrolled = displayViewLine(c);
00407
00408 m_startPos.setPos(c);
00409
00410
00411 m_madeVisible = false;
00412
00413 if (viewLinesScrolledUsable)
00414 {
00415 int lines = linesDisplayed();
00416 if ((int)m_doc->numVisLines() < lines) {
00417 KateTextCursor end(m_doc->numVisLines() - 1, m_doc->lineLength(m_doc->getRealLine(m_doc->numVisLines() - 1)));
00418 lines = kMin((int)linesDisplayed(), displayViewLine(end) + 1);
00419 }
00420
00421 Q_ASSERT(lines >= 0);
00422
00423 if (!calledExternally && QABS(viewLinesScrolled) < lines)
00424 {
00425 updateView(false, viewLinesScrolled);
00426
00427 int scrollHeight = -(viewLinesScrolled * (int)m_view->renderer()->fontHeight());
00428 int scrollbarWidth = style().scrollBarExtent().width();
00429
00430
00431
00432
00433 scroll(0, scrollHeight);
00434 update(0, height()+scrollHeight-scrollbarWidth, width(), 2*scrollbarWidth);
00435
00436 leftBorder->scroll(0, scrollHeight);
00437 leftBorder->update(0, leftBorder->height()+scrollHeight-scrollbarWidth, leftBorder->width(), 2*scrollbarWidth);
00438
00439 return;
00440 }
00441 }
00442
00443 updateView();
00444 update();
00445 leftBorder->update();
00446 }
00447
00448 void KateViewInternal::scrollColumns ( int x )
00449 {
00450 if (x == m_startX)
00451 return;
00452
00453 if (x < 0)
00454 x = 0;
00455
00456 int dx = m_startX - x;
00457 m_startX = x;
00458
00459 if (QABS(dx) < width())
00460 scroll(dx, 0);
00461 else
00462 update();
00463
00464 m_columnScroll->blockSignals(true);
00465 m_columnScroll->setValue(m_startX);
00466 m_columnScroll->blockSignals(false);
00467 }
00468
00469
00470 void KateViewInternal::updateView(bool changed, int viewLinesScrolled)
00471 {
00472 m_updatingView = true;
00473
00474 uint contentLines = m_doc->visibleLines();
00475
00476 m_lineScroll->blockSignals(true);
00477
00478 KateTextCursor maxStart = maxStartPos(changed);
00479 int maxLineScrollRange = maxStart.line();
00480 if (m_view->dynWordWrap() && maxStart.col() != 0)
00481 maxLineScrollRange++;
00482 m_lineScroll->setRange(0, maxLineScrollRange);
00483
00484 m_lineScroll->setValue(startPos().line());
00485 m_lineScroll->setSteps(1, height() / m_view->renderer()->fontHeight());
00486 m_lineScroll->blockSignals(false);
00487
00488 uint oldSize = lineRanges.size ();
00489 uint newSize = (height() / m_view->renderer()->fontHeight()) + 1;
00490 if (oldSize != newSize) {
00491 lineRanges.resize((height() / m_view->renderer()->fontHeight()) + 1);
00492 if (newSize > oldSize) {
00493 static KateLineRange blank;
00494 for (uint i = oldSize; i < newSize; i++) {
00495 lineRanges[i] = blank;
00496 }
00497 }
00498 }
00499
00500 if (oldSize < lineRanges.size ())
00501 {
00502 for (uint i=oldSize; i < lineRanges.size(); i++)
00503 lineRanges[i].dirty = true;
00504 }
00505
00506
00507 if (viewLinesScrolled != 0) {
00508
00509 bool forwards = viewLinesScrolled >= 0 ? true : false;
00510 for (uint z = forwards ? 0 : lineRanges.count() - 1; z < lineRanges.count(); forwards ? z++ : z--) {
00511 uint oldZ = z + viewLinesScrolled;
00512 if (oldZ < lineRanges.count()) {
00513 lineRanges[z] = lineRanges[oldZ];
00514 } else {
00515 lineRanges[z].dirty = true;
00516 }
00517 }
00518 }
00519
00520 if (m_view->dynWordWrap())
00521 {
00522 KateTextCursor realStart = startPos();
00523 realStart.setLine(m_doc->getRealLine(realStart.line()));
00524
00525 KateLineRange startRange = range(realStart);
00526 uint line = startRange.virtualLine;
00527 int realLine = startRange.line;
00528 uint oldLine = line;
00529 int startCol = startRange.startCol;
00530 int startX = startRange.startX;
00531 int endX = startRange.startX;
00532 int shiftX = startRange.startCol ? startRange.shiftX : 0;
00533 bool wrap = false;
00534 int newViewLine = startRange.viewLine;
00535
00536 KateTextLine::Ptr text = textLine(realLine);
00537
00538 bool alreadyDirty = false;
00539
00540 for (uint z = 0; z < lineRanges.size(); z++)
00541 {
00542 if (oldLine != line) {
00543 realLine = (int)m_doc->getRealLine(line);
00544
00545 if (z)
00546 lineRanges[z-1].startsInvisibleBlock = (realLine != lineRanges[z-1].line + 1);
00547
00548 text = textLine(realLine);
00549 startCol = 0;
00550 startX = 0;
00551 endX = 0;
00552 shiftX = 0;
00553 newViewLine = 0;
00554 oldLine = line;
00555 }
00556
00557 if (line >= contentLines || !text)
00558 {
00559 if (lineRanges[z].line != -1)
00560 lineRanges[z].dirty = true;
00561
00562 lineRanges[z].clear();
00563
00564 line++;
00565 }
00566 else
00567 {
00568 if (lineRanges[z].line != realLine || lineRanges[z].startCol != startCol)
00569 alreadyDirty = lineRanges[z].dirty = true;
00570
00571 if (lineRanges[z].dirty || changed || alreadyDirty) {
00572 alreadyDirty = true;
00573
00574 lineRanges[z].virtualLine = line;
00575 lineRanges[z].line = realLine;
00576 lineRanges[z].startsInvisibleBlock = false;
00577
00578 int tempEndX = 0;
00579
00580 int endCol = m_view->renderer()->textWidth(text, startCol, width() - shiftX, &wrap, &tempEndX);
00581
00582 endX += tempEndX;
00583
00584 if (wrap)
00585 {
00586 if (m_view->config()->dynWordWrapAlignIndent() > 0)
00587 {
00588 if (startX == 0)
00589 {
00590 int pos = text->nextNonSpaceChar(0);
00591
00592 if (pos > 0)
00593 shiftX = m_view->renderer()->textWidth(text, pos);
00594
00595 if (shiftX > ((double)width() / 100 * m_view->config()->dynWordWrapAlignIndent()))
00596 shiftX = 0;
00597 }
00598 }
00599
00600 if ((lineRanges[z].startX != startX) || (lineRanges[z].endX != endX) ||
00601 (lineRanges[z].startCol != startCol) || (lineRanges[z].endCol != endCol) ||
00602 (lineRanges[z].shiftX != shiftX))
00603 lineRanges[z].dirty = true;
00604
00605 lineRanges[z].startCol = startCol;
00606 lineRanges[z].endCol = endCol;
00607 lineRanges[z].startX = startX;
00608 lineRanges[z].endX = endX;
00609 lineRanges[z].viewLine = newViewLine;
00610 lineRanges[z].wrap = true;
00611
00612 startCol = endCol;
00613 startX = endX;
00614 }
00615 else
00616 {
00617 if ((lineRanges[z].startX != startX) || (lineRanges[z].endX != endX) ||
00618 (lineRanges[z].startCol != startCol) || (lineRanges[z].endCol != endCol))
00619 lineRanges[z].dirty = true;
00620
00621 lineRanges[z].startCol = startCol;
00622 lineRanges[z].endCol = endCol;
00623 lineRanges[z].startX = startX;
00624 lineRanges[z].endX = endX;
00625 lineRanges[z].viewLine = newViewLine;
00626 lineRanges[z].wrap = false;
00627
00628 line++;
00629 }
00630
00631 lineRanges[z].shiftX = shiftX;
00632
00633 } else {
00634
00635 if (lineRanges[z].wrap) {
00636 startCol = lineRanges[z].endCol;
00637 startX = lineRanges[z].endX;
00638 endX = lineRanges[z].endX;
00639 } else {
00640 line++;
00641 }
00642 shiftX = lineRanges[z].shiftX;
00643 }
00644 }
00645 newViewLine++;
00646 }
00647 }
00648 else
00649 {
00650 uint z = 0;
00651
00652 for(; (z + startLine() < contentLines) && (z < lineRanges.size()); z++)
00653 {
00654 if (lineRanges[z].dirty || lineRanges[z].line != (int)m_doc->getRealLine(z + startLine())) {
00655 lineRanges[z].dirty = true;
00656
00657 lineRanges[z].line = m_doc->getRealLine( z + startLine() );
00658 if (z)
00659 lineRanges[z-1].startsInvisibleBlock = (lineRanges[z].line != lineRanges[z-1].line + 1);
00660
00661 lineRanges[z].virtualLine = z + startLine();
00662 lineRanges[z].startCol = 0;
00663 lineRanges[z].endCol = m_doc->lineLength(lineRanges[z].line);
00664 lineRanges[z].startX = 0;
00665 lineRanges[z].endX = m_view->renderer()->textWidth( textLine( lineRanges[z].line ), -1 );
00666 lineRanges[z].shiftX = 0;
00667 lineRanges[z].viewLine = 0;
00668 lineRanges[z].wrap = false;
00669 }
00670 else if (z && lineRanges[z-1].dirty)
00671 {
00672 lineRanges[z-1].startsInvisibleBlock = (lineRanges[z].line != lineRanges[z-1].line + 1);
00673 }
00674 }
00675
00676 for (; z < lineRanges.size(); z++)
00677 {
00678 if (lineRanges[z].line != -1)
00679 lineRanges[z].dirty = true;
00680
00681 lineRanges[z].clear();
00682 }
00683
00684 int max = maxLen(startLine()) - width();
00685 if (max < 0)
00686 max = 0;
00687
00688
00689 if (max == 0)
00690 {
00691 scrollColumns(0);
00692 }
00693
00694 m_columnScroll->blockSignals(true);
00695
00696
00697 m_columnScroll->setDisabled (max == 0);
00698
00699 m_columnScroll->setRange(0, max);
00700
00701 m_columnScroll->setValue(m_startX);
00702
00703
00704 m_columnScroll->setSteps(m_view->renderer()->config()->fontMetrics()->width('a'), width());
00705
00706 m_columnScroll->blockSignals(false);
00707 }
00708
00709 m_updatingView = false;
00710
00711 if (changed)
00712 paintText(0, 0, width(), height(), true);
00713 }
00714
00715 void KateViewInternal::paintText (int x, int y, int width, int height, bool paintOnlyDirty)
00716 {
00717
00718 int xStart = startX() + x;
00719 int xEnd = xStart + width;
00720 uint h = m_view->renderer()->fontHeight();
00721 uint startz = (y / h);
00722 uint endz = startz + 1 + (height / h);
00723 uint lineRangesSize = lineRanges.size();
00724
00725 static QPixmap drawBuffer;
00726
00727 if (drawBuffer.width() < KateViewInternal::width() || drawBuffer.height() < (int)h)
00728 drawBuffer.resize(KateViewInternal::width(), (int)h);
00729
00730 if (drawBuffer.isNull())
00731 return;
00732
00733 QPainter paint(this);
00734 QPainter paintDrawBuffer(&drawBuffer);
00735
00736
00737 m_view->renderer()->setCaretStyle(m_view->isOverwriteMode() ? KateRenderer::Replace : KateRenderer::Insert);
00738 m_view->renderer()->setShowTabs(m_doc->configFlags() & KateDocument::cfShowTabs);
00739
00740 for (uint z=startz; z <= endz; z++)
00741 {
00742 if ( (z >= lineRangesSize) || ((lineRanges[z].line == -1) && (!paintOnlyDirty || lineRanges[z].dirty)) )
00743 {
00744 if (!(z >= lineRangesSize))
00745 lineRanges[z].dirty = false;
00746
00747 paint.fillRect( x, z * h, width, h, m_view->renderer()->config()->backgroundColor() );
00748 }
00749 else if (!paintOnlyDirty || lineRanges[z].dirty)
00750 {
00751 lineRanges[z].dirty = false;
00752
00753 m_view->renderer()->paintTextLine(paintDrawBuffer, &lineRanges[z], xStart, xEnd, &cursor, &bm);
00754
00755 paint.drawPixmap (x, z * h, drawBuffer, 0, 0, width, h);
00756 }
00757 }
00758 }
00759
00764 void KateViewInternal::makeVisible (const KateTextCursor& c, uint endCol, bool force, bool center, bool calledExternally)
00765 {
00766
00767
00768
00769
00770
00771 if ( force )
00772 {
00773 KateTextCursor scroll = c;
00774 scrollPos(scroll, force, calledExternally);
00775 }
00776 else if (center && (c < startPos() || c > endPos()))
00777 {
00778 KateTextCursor scroll = viewLineOffset(c, -int(linesDisplayed()) / 2);
00779 scrollPos(scroll, false, calledExternally);
00780 }
00781 else if ( c > viewLineOffset(endPos(), -m_minLinesVisible) )
00782 {
00783 KateTextCursor scroll = viewLineOffset(c, -((int)linesDisplayed() - m_minLinesVisible - 1));
00784 scrollPos(scroll, false, calledExternally);
00785 }
00786 else if ( c < viewLineOffset(startPos(), m_minLinesVisible) )
00787 {
00788 KateTextCursor scroll = viewLineOffset(c, -m_minLinesVisible);
00789 scrollPos(scroll, false, calledExternally);
00790 }
00791 else
00792 {
00793
00794 KateTextCursor max = maxStartPos();
00795 if (startPos() > max) {
00796 scrollPos(max, max.col(), calledExternally);
00797 }
00798 }
00799
00800 if (!m_view->dynWordWrap() && endCol != (uint)-1)
00801 {
00802 int sX = (int)m_view->renderer()->textWidth (textLine( m_doc->getRealLine( c.line() ) ), c.col() );
00803
00804 int sXborder = sX-8;
00805 if (sXborder < 0)
00806 sXborder = 0;
00807
00808 if (sX < m_startX)
00809 scrollColumns (sXborder);
00810 else if (sX > m_startX + width())
00811 scrollColumns (sX - width() + 8);
00812 }
00813
00814 m_madeVisible = !force;
00815 }
00816
00817 void KateViewInternal::slotRegionVisibilityChangedAt(unsigned int)
00818 {
00819 kdDebug(13030) << "slotRegionVisibilityChangedAt()" << endl;
00820 m_cachedMaxStartPos.setLine(-1);
00821 KateTextCursor max = maxStartPos();
00822 if (startPos() > max)
00823 scrollPos(max);
00824
00825 updateView();
00826 update();
00827 leftBorder->update();
00828 }
00829
00830 void KateViewInternal::slotCodeFoldingChanged()
00831 {
00832 leftBorder->update();
00833 }
00834
00835 void KateViewInternal::slotRegionBeginEndAddedRemoved(unsigned int)
00836 {
00837 kdDebug(13030) << "slotRegionBeginEndAddedRemoved()" << endl;
00838
00839 leftBorder->update();
00840 }
00841
00842 void KateViewInternal::showEvent ( QShowEvent *e )
00843 {
00844 updateView ();
00845
00846 QWidget::showEvent (e);
00847 }
00848
00849 uint KateViewInternal::linesDisplayed() const
00850 {
00851 int h = height();
00852 int fh = m_view->renderer()->fontHeight();
00853
00854 return (h - (h % fh)) / fh;
00855 }
00856
00857 QPoint KateViewInternal::cursorCoordinates()
00858 {
00859 int viewLine = displayViewLine(displayCursor, true);
00860
00861 if (viewLine == -1)
00862 return QPoint(-1, -1);
00863
00864 uint y = viewLine * m_view->renderer()->fontHeight();
00865 uint x = cXPos - m_startX - lineRanges[viewLine].startX + leftBorder->width() + lineRanges[viewLine].xOffset();
00866
00867 return QPoint(x, y);
00868 }
00869
00870 void KateViewInternal::updateMicroFocusHint()
00871 {
00872 int line = displayViewLine(displayCursor, true);
00873
00874
00875
00876 if (line == -1 || !hasFocus())
00877 return;
00878
00879 KateRenderer *renderer = m_view->renderer();
00880
00881
00882
00883
00884
00885
00886 uint preeditStrLen = renderer->textWidth(textLine(m_imPreeditStartLine), cursor.col()) - renderer->textWidth(textLine(m_imPreeditStartLine), m_imPreeditSelStart);
00887 uint x = cXPos - m_startX - lineRanges[line].startX + lineRanges[line].xOffset() - preeditStrLen;
00888 uint y = line * renderer->fontHeight();
00889
00890 setMicroFocusHint(x, y, 0, renderer->fontHeight());
00891 }
00892
00893 void KateViewInternal::doReturn()
00894 {
00895 KateTextCursor c = cursor;
00896 m_doc->newLine( c, this );
00897 updateCursor( c );
00898 updateView();
00899 }
00900
00901 void KateViewInternal::doDelete()
00902 {
00903 m_doc->del( m_view, cursor );
00904 if (m_view->m_codeCompletion->codeCompletionVisible()) {
00905 m_view->m_codeCompletion->updateBox();
00906 }
00907 }
00908
00909 void KateViewInternal::doBackspace()
00910 {
00911 m_doc->backspace( m_view, cursor );
00912 if (m_view->m_codeCompletion->codeCompletionVisible()) {
00913 m_view->m_codeCompletion->updateBox();
00914 }
00915 }
00916
00917 void KateViewInternal::doTranspose()
00918 {
00919 m_doc->transpose( cursor );
00920 }
00921
00922 void KateViewInternal::doDeleteWordLeft()
00923 {
00924 wordLeft( true );
00925 m_view->removeSelectedText();
00926 update();
00927 }
00928
00929 void KateViewInternal::doDeleteWordRight()
00930 {
00931 wordRight( true );
00932 m_view->removeSelectedText();
00933 update();
00934 }
00935
00936 class CalculatingCursor : public KateTextCursor {
00937 public:
00938 CalculatingCursor(KateViewInternal* vi)
00939 : KateTextCursor()
00940 , m_vi(vi)
00941 {
00942 Q_ASSERT(valid());
00943 }
00944
00945 CalculatingCursor(KateViewInternal* vi, const KateTextCursor& c)
00946 : KateTextCursor(c)
00947 , m_vi(vi)
00948 {
00949 Q_ASSERT(valid());
00950 }
00951
00952
00953 CalculatingCursor(KateViewInternal* vi, uint line, uint col)
00954 : KateTextCursor(line, col)
00955 , m_vi(vi)
00956 {
00957 makeValid();
00958 }
00959
00960
00961 virtual CalculatingCursor& operator+=( int n ) = 0;
00962
00963 virtual CalculatingCursor& operator-=( int n ) = 0;
00964
00965 CalculatingCursor& operator++() { return operator+=( 1 ); }
00966
00967 CalculatingCursor& operator--() { return operator-=( 1 ); }
00968
00969 void makeValid() {
00970 m_line = kMax( 0, kMin( int( m_vi->m_doc->numLines() - 1 ), line() ) );
00971 if (m_vi->m_view->wrapCursor())
00972 m_col = kMax( 0, kMin( m_vi->m_doc->lineLength( line() ), col() ) );
00973 else
00974 m_col = kMax( 0, col() );
00975 Q_ASSERT( valid() );
00976 }
00977
00978 void toEdge( Bias bias ) {
00979 if( bias == left ) m_col = 0;
00980 else if( bias == right ) m_col = m_vi->m_doc->lineLength( line() );
00981 }
00982
00983 bool atEdge() const { return atEdge( left ) || atEdge( right ); }
00984
00985 bool atEdge( Bias bias ) const {
00986 switch( bias ) {
00987 case left: return col() == 0;
00988 case none: return atEdge();
00989 case right: return col() == m_vi->m_doc->lineLength( line() );
00990 default: Q_ASSERT(false); return false;
00991 }
00992 }
00993
00994 protected:
00995 bool valid() const {
00996 return line() >= 0 &&
00997 uint( line() ) < m_vi->m_doc->numLines() &&
00998 col() >= 0 &&
00999 (!m_vi->m_view->wrapCursor() || col() <= m_vi->m_doc->lineLength( line() ));
01000 }
01001 KateViewInternal* m_vi;
01002 };
01003
01004 class BoundedCursor : public CalculatingCursor {
01005 public:
01006 BoundedCursor(KateViewInternal* vi)
01007 : CalculatingCursor( vi ) {};
01008 BoundedCursor(KateViewInternal* vi, const KateTextCursor& c )
01009 : CalculatingCursor( vi, c ) {};
01010 BoundedCursor(KateViewInternal* vi, uint line, uint col )
01011 : CalculatingCursor( vi, line, col ) {};
01012 virtual CalculatingCursor& operator+=( int n ) {
01013 m_col += n;
01014
01015 if (n > 0 && m_vi->m_view->dynWordWrap()) {
01016
01017 if (m_col > m_vi->m_doc->lineLength(m_line)) {
01018 KateLineRange currentRange = m_vi->range(*this);
01019
01020 int endX;
01021 bool crap;
01022 m_vi->m_view->renderer()->textWidth(m_vi->textLine(m_line), currentRange.startCol, m_vi->width() - currentRange.xOffset(), &crap, &endX);
01023 endX += (m_col - currentRange.endCol + 1) * m_vi->m_view->renderer()->spaceWidth();
01024
01025
01026 if (endX >= m_vi->width() - currentRange.xOffset()) {
01027 m_col -= n;
01028 if ( uint( line() ) < m_vi->m_doc->numLines() - 1 ) {
01029 m_line++;
01030 m_col = 0;
01031 }
01032 }
01033 }
01034
01035 } else if (n < 0 && col() < 0 && line() > 0 ) {
01036 m_line--;
01037 m_col = m_vi->m_doc->lineLength( line() );
01038 }
01039
01040 m_col = kMax( 0, col() );
01041
01042 Q_ASSERT( valid() );
01043 return *this;
01044 }
01045 virtual CalculatingCursor& operator-=( int n ) {
01046 return operator+=( -n );
01047 }
01048 };
01049
01050 class WrappingCursor : public CalculatingCursor {
01051 public:
01052 WrappingCursor(KateViewInternal* vi)
01053 : CalculatingCursor( vi) {};
01054 WrappingCursor(KateViewInternal* vi, const KateTextCursor& c )
01055 : CalculatingCursor( vi, c ) {};
01056 WrappingCursor(KateViewInternal* vi, uint line, uint col )
01057 : CalculatingCursor( vi, line, col ) {};
01058
01059 virtual CalculatingCursor& operator+=( int n ) {
01060 if( n < 0 ) return operator-=( -n );
01061 int len = m_vi->m_doc->lineLength( line() );
01062 if( col() + n <= len ) {
01063 m_col += n;
01064 } else if( uint( line() ) < m_vi->m_doc->numLines() - 1 ) {
01065 n -= len - col() + 1;
01066 m_col = 0;
01067 m_line++;
01068 operator+=( n );
01069 } else {
01070 m_col = len;
01071 }
01072 Q_ASSERT( valid() );
01073 return *this;
01074 }
01075 virtual CalculatingCursor& operator-=( int n ) {
01076 if( n < 0 ) return operator+=( -n );
01077 if( col() - n >= 0 ) {
01078 m_col -= n;
01079 } else if( line() > 0 ) {
01080 n -= col() + 1;
01081 m_line--;
01082 m_col = m_vi->m_doc->lineLength( line() );
01083 operator-=( n );
01084 } else {
01085 m_col = 0;
01086 }
01087 Q_ASSERT( valid() );
01088 return *this;
01089 }
01090 };
01091
01092 void KateViewInternal::moveChar( Bias bias, bool sel )
01093 {
01094 KateTextCursor c;
01095 if ( m_view->wrapCursor() ) {
01096 c = WrappingCursor( this, cursor ) += bias;
01097 } else {
01098 c = BoundedCursor( this, cursor ) += bias;
01099 }
01100
01101 updateSelection( c, sel );
01102 updateCursor( c );
01103 }
01104
01105 void KateViewInternal::cursorLeft( bool sel )
01106 {
01107 if ( ! m_view->wrapCursor() && cursor.col() == 0 )
01108 return;
01109
01110 moveChar( left, sel );
01111 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01112 m_view->m_codeCompletion->updateBox();
01113 }
01114 }
01115
01116 void KateViewInternal::cursorRight( bool sel )
01117 {
01118 moveChar( right, sel );
01119 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01120 m_view->m_codeCompletion->updateBox();
01121 }
01122 }
01123
01124 void KateViewInternal::wordLeft ( bool sel )
01125 {
01126 WrappingCursor c( this, cursor );
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136 KateHighlighting* h = m_doc->highlight();
01137 if( !c.atEdge( left ) ) {
01138
01139 while( !c.atEdge( left ) && m_doc->textLine( c.line() )[ c.col() - 1 ].isSpace() )
01140 --c;
01141 }
01142 if( c.atEdge( left ) )
01143 {
01144 --c;
01145 }
01146 else if( h->isInWord( m_doc->textLine( c.line() )[ c.col() - 1 ] ) )
01147 {
01148 while( !c.atEdge( left ) && h->isInWord( m_doc->textLine( c.line() )[ c.col() - 1 ] ) )
01149 --c;
01150 }
01151 else
01152 {
01153 while( !c.atEdge( left )
01154 && !h->isInWord( m_doc->textLine( c.line() )[ c.col() - 1 ] )
01155
01156
01157 && !m_doc->textLine( c.line() )[ c.col() - 1 ].isSpace() )
01158 {
01159 --c;
01160 }
01161 }
01162
01163 updateSelection( c, sel );
01164 updateCursor( c );
01165 }
01166
01167 void KateViewInternal::wordRight( bool sel )
01168 {
01169 WrappingCursor c( this, cursor );
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179 KateHighlighting* h = m_doc->highlight();
01180 if( c.atEdge( right ) )
01181 {
01182 ++c;
01183 }
01184 else if( h->isInWord( m_doc->textLine( c.line() )[ c.col() ] ) )
01185 {
01186 while( !c.atEdge( right ) && h->isInWord( m_doc->textLine( c.line() )[ c.col() ] ) )
01187 ++c;
01188 }
01189 else
01190 {
01191 while( !c.atEdge( right )
01192 && !h->isInWord( m_doc->textLine( c.line() )[ c.col() ] )
01193
01194
01195 && !m_doc->textLine( c.line() )[ c.col() ].isSpace() )
01196 {
01197 ++c;
01198 }
01199 }
01200
01201 while( !c.atEdge( right ) && m_doc->textLine( c.line() )[ c.col() ].isSpace() )
01202 ++c;
01203
01204 updateSelection( c, sel );
01205 updateCursor( c );
01206 }
01207
01208 void KateViewInternal::moveEdge( Bias bias, bool sel )
01209 {
01210 BoundedCursor c( this, cursor );
01211 c.toEdge( bias );
01212 updateSelection( c, sel );
01213 updateCursor( c );
01214 }
01215
01216 void KateViewInternal::home( bool sel )
01217 {
01218 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01219 QKeyEvent e(QEvent::KeyPress, Qt::Key_Home, 0, 0);
01220 m_view->m_codeCompletion->handleKey(&e);
01221 return;
01222 }
01223
01224 if (m_view->dynWordWrap() && currentRange().startCol) {
01225
01226 if (cursor.col() != currentRange().startCol) {
01227 KateTextCursor c(cursor.line(), currentRange().startCol);
01228 updateSelection( c, sel );
01229 updateCursor( c );
01230 return;
01231 }
01232 }
01233
01234 if( !(m_doc->configFlags() & KateDocument::cfSmartHome) ) {
01235 moveEdge( left, sel );
01236 return;
01237 }
01238
01239 KateTextLine::Ptr l = textLine( cursor.line() );
01240
01241 if (!l)
01242 return;
01243
01244 KateTextCursor c = cursor;
01245 int lc = l->firstChar();
01246
01247 if( lc < 0 || c.col() == lc ) {
01248 c.setCol(0);
01249 } else {
01250 c.setCol(lc);
01251 }
01252
01253 updateSelection( c, sel );
01254 updateCursor( c, true );
01255 }
01256
01257 void KateViewInternal::end( bool sel )
01258 {
01259 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01260 QKeyEvent e(QEvent::KeyPress, Qt::Key_End, 0, 0);
01261 m_view->m_codeCompletion->handleKey(&e);
01262 return;
01263 }
01264
01265 KateLineRange range = currentRange();
01266
01267 if (m_view->dynWordWrap() && range.wrap) {
01268
01269 if (cursor.col() < range.endCol - 1) {
01270 KateTextCursor c(cursor.line(), range.endCol - 1);
01271 updateSelection( c, sel );
01272 updateCursor( c );
01273 return;
01274 }
01275 }
01276
01277 if( !(m_doc->configFlags() & KateDocument::cfSmartHome) ) {
01278 moveEdge( right, sel );
01279 return;
01280 }
01281
01282 KateTextLine::Ptr l = textLine( cursor.line() );
01283
01284 if (!l)
01285 return;
01286
01287
01288 KateTextCursor c = cursor;
01289
01290
01291
01292 if (c.col() == m_doc->lineLength(c.line())) {
01293 c.setCol(l->lastChar() + 1);
01294 updateSelection(c, sel);
01295 updateCursor(c, true);
01296 } else {
01297 moveEdge(right, sel);
01298 }
01299 }
01300
01301 KateLineRange KateViewInternal::range(int realLine, const KateLineRange* previous)
01302 {
01303
01304 if (!m_updatingView && realLine >= lineRanges[0].line && realLine <= lineRanges[lineRanges.count() - 1].line)
01305 for (uint i = 0; i < lineRanges.count(); i++)
01306 if (realLine == lineRanges[i].line)
01307 if (!m_view->dynWordWrap() || (!previous && lineRanges[i].startCol == 0) || (previous && lineRanges[i].startCol == previous->endCol))
01308 return lineRanges[i];
01309
01310
01311 KateLineRange ret;
01312
01313 KateTextLine::Ptr text = textLine(realLine);
01314 if (!text) {
01315 return KateLineRange();
01316 }
01317
01318 if (!m_view->dynWordWrap()) {
01319 Q_ASSERT(!previous);
01320 ret.line = realLine;
01321 ret.virtualLine = m_doc->getVirtualLine(realLine);
01322 ret.startCol = 0;
01323 ret.endCol = m_doc->lineLength(realLine);
01324 ret.startX = 0;
01325 ret.endX = m_view->renderer()->textWidth(text, -1);
01326 ret.viewLine = 0;
01327 ret.wrap = false;
01328 return ret;
01329 }
01330
01331 ret.endCol = (int)m_view->renderer()->textWidth(text, previous ? previous->endCol : 0, width() - (previous ? previous->shiftX : 0), &ret.wrap, &ret.endX);
01332
01333 Q_ASSERT(ret.endCol > ret.startCol);
01334
01335 ret.line = realLine;
01336
01337 if (previous) {
01338 ret.virtualLine = previous->virtualLine;
01339 ret.startCol = previous->endCol;
01340 ret.startX = previous->endX;
01341 ret.endX += previous->endX;
01342 ret.shiftX = previous->shiftX;
01343 ret.viewLine = previous->viewLine + 1;
01344
01345 } else {
01346
01347 if (m_view->config()->dynWordWrapAlignIndent() > 0) {
01348 int pos = text->nextNonSpaceChar(0);
01349
01350 if (pos > 0)
01351 ret.shiftX = m_view->renderer()->textWidth(text, pos);
01352
01353 if (ret.shiftX > ((double)width() / 100 * m_view->config()->dynWordWrapAlignIndent()))
01354 ret.shiftX = 0;
01355 }
01356
01357 ret.virtualLine = m_doc->getVirtualLine(realLine);
01358 ret.startCol = 0;
01359 ret.startX = 0;
01360 ret.viewLine = 0;
01361 }
01362
01363 return ret;
01364 }
01365
01366 KateLineRange KateViewInternal::currentRange()
01367 {
01368
01369
01370 return range(cursor);
01371 }
01372
01373 KateLineRange KateViewInternal::previousRange()
01374 {
01375 uint currentViewLine = viewLine(cursor);
01376
01377 if (currentViewLine)
01378 return range(cursor.line(), currentViewLine - 1);
01379 else
01380 return range(m_doc->getRealLine(displayCursor.line() - 1), -1);
01381 }
01382
01383 KateLineRange KateViewInternal::nextRange()
01384 {
01385 uint currentViewLine = viewLine(cursor) + 1;
01386
01387 if (currentViewLine >= viewLineCount(cursor.line())) {
01388 currentViewLine = 0;
01389 return range(cursor.line() + 1, currentViewLine);
01390 } else {
01391 return range(cursor.line(), currentViewLine);
01392 }
01393 }
01394
01395 KateLineRange KateViewInternal::range(const KateTextCursor& realCursor)
01396 {
01397
01398
01399 KateLineRange thisRange;
01400 bool first = true;
01401
01402 do {
01403 thisRange = range(realCursor.line(), first ? 0L : &thisRange);
01404 first = false;
01405 } while (thisRange.wrap && !(realCursor.col() >= thisRange.startCol && realCursor.col() < thisRange.endCol) && thisRange.startCol != thisRange.endCol);
01406
01407 return thisRange;
01408 }
01409
01410 KateLineRange KateViewInternal::range(uint realLine, int viewLine)
01411 {
01412
01413
01414 KateLineRange thisRange;
01415 bool first = true;
01416
01417 do {
01418 thisRange = range(realLine, first ? 0L : &thisRange);
01419 first = false;
01420 } while (thisRange.wrap && viewLine != thisRange.viewLine && thisRange.startCol != thisRange.endCol);
01421
01422 if (viewLine != -1 && viewLine != thisRange.viewLine)
01423 kdDebug(13030) << "WARNING: viewLine " << viewLine << " of line " << realLine << " does not exist." << endl;
01424
01425 return thisRange;
01426 }
01427
01433 uint KateViewInternal::viewLine(const KateTextCursor& realCursor)
01434 {
01435 if (!m_view->dynWordWrap()) return 0;
01436
01437 if (realCursor.col() == 0) return 0;
01438
01439 KateLineRange thisRange;
01440 bool first = true;
01441
01442 do {
01443 thisRange = range(realCursor.line(), first ? 0L : &thisRange);
01444 first = false;
01445 } while (thisRange.wrap && !(realCursor.col() >= thisRange.startCol && realCursor.col() < thisRange.endCol) && thisRange.startCol != thisRange.endCol);
01446
01447 return thisRange.viewLine;
01448 }
01449
01450 int KateViewInternal::displayViewLine(const KateTextCursor& virtualCursor, bool limitToVisible)
01451 {
01452 KateTextCursor work = startPos();
01453
01454 int limit = linesDisplayed();
01455
01456
01457 if (!m_view->dynWordWrap()) {
01458 int ret = virtualCursor.line() - startLine();
01459 if (limitToVisible && (ret < 0 || ret > limit))
01460 return -1;
01461 else
01462 return ret;
01463 }
01464
01465 if (work == virtualCursor) {
01466 return 0;
01467 }
01468
01469 int ret = -(int)viewLine(work);
01470 bool forwards = (work < virtualCursor) ? true : false;
01471
01472
01473 if (forwards) {
01474 while (work.line() != virtualCursor.line()) {
01475 ret += viewLineCount(m_doc->getRealLine(work.line()));
01476 work.setLine(work.line() + 1);
01477 if (limitToVisible && ret > limit)
01478 return -1;
01479 }
01480 } else {
01481 while (work.line() != virtualCursor.line()) {
01482 work.setLine(work.line() - 1);
01483 ret -= viewLineCount(m_doc->getRealLine(work.line()));
01484 if (limitToVisible && ret < 0)
01485 return -1;
01486 }
01487 }
01488
01489
01490 KateTextCursor realCursor = virtualCursor;
01491 realCursor.setLine(m_doc->getRealLine(realCursor.line()));
01492 if (realCursor.col() == -1) realCursor.setCol(m_doc->lineLength(realCursor.line()));
01493 ret += viewLine(realCursor);
01494
01495 if (limitToVisible && (ret < 0 || ret > limit))
01496 return -1;
01497
01498 return ret;
01499 }
01500
01501 uint KateViewInternal::lastViewLine(uint realLine)
01502 {
01503 if (!m_view->dynWordWrap()) return 0;
01504
01505 KateLineRange thisRange;
01506 bool first = true;
01507
01508 do {
01509 thisRange = range(realLine, first ? 0L : &thisRange);
01510 first = false;
01511 } while (thisRange.wrap && thisRange.startCol != thisRange.endCol);
01512
01513 return thisRange.viewLine;
01514 }
01515
01516 uint KateViewInternal::viewLineCount(uint realLine)
01517 {
01518 return lastViewLine(realLine) + 1;
01519 }
01520
01521
01522
01523
01524
01525
01526
01527
01528 KateTextCursor KateViewInternal::viewLineOffset(const KateTextCursor& virtualCursor, int offset, bool keepX)
01529 {
01530 if (!m_view->dynWordWrap()) {
01531 KateTextCursor ret(kMin((int)m_doc->visibleLines() - 1, virtualCursor.line() + offset), 0);
01532
01533 if (ret.line() < 0)
01534 ret.setLine(0);
01535
01536 if (keepX) {
01537 int realLine = m_doc->getRealLine(ret.line());
01538 ret.setCol(m_doc->lineLength(realLine) - 1);
01539
01540 if (m_currentMaxX > cXPos)
01541 cXPos = m_currentMaxX;
01542
01543 if (m_view->wrapCursor())
01544 cXPos = kMin(cXPos, (int)m_view->renderer()->textWidth(textLine(realLine), m_doc->lineLength(realLine)));
01545
01546 m_view->renderer()->textWidth(ret, cXPos);
01547 }
01548
01549 return ret;
01550 }
01551
01552 KateTextCursor realCursor = virtualCursor;
01553 realCursor.setLine(m_doc->getRealLine(virtualCursor.line()));
01554
01555 uint cursorViewLine = viewLine(realCursor);
01556
01557 int currentOffset = 0;
01558 int virtualLine = 0;
01559
01560 bool forwards = (offset > 0) ? true : false;
01561
01562 if (forwards) {
01563 currentOffset = lastViewLine(realCursor.line()) - cursorViewLine;
01564 if (offset <= currentOffset) {
01565
01566 KateLineRange thisRange = range(realCursor.line(), cursorViewLine + offset);
01567 Q_ASSERT(thisRange.virtualLine == virtualCursor.line());
01568 return KateTextCursor(virtualCursor.line(), thisRange.startCol);
01569 }
01570
01571 virtualLine = virtualCursor.line() + 1;
01572
01573 } else {
01574 offset = -offset;
01575 currentOffset = cursorViewLine;
01576 if (offset <= currentOffset) {
01577
01578 KateLineRange thisRange = range(realCursor.line(), cursorViewLine - offset);
01579 Q_ASSERT(thisRange.virtualLine == virtualCursor.line());
01580 return KateTextCursor(virtualCursor.line(), thisRange.startCol);
01581 }
01582
01583 virtualLine = virtualCursor.line() - 1;
01584 }
01585
01586 currentOffset++;
01587
01588 while (virtualLine >= 0 && virtualLine < (int)m_doc->visibleLines())
01589 {
01590 KateLineRange thisRange;
01591 bool first = true;
01592 int realLine = m_doc->getRealLine(virtualLine);
01593
01594 do {
01595 thisRange = range(realLine, first ? 0L : &thisRange);
01596 first = false;
01597
01598 if (offset == currentOffset) {
01599 if (!forwards) {
01600
01601 int requiredViewLine = lastViewLine(realLine) - thisRange.viewLine;
01602 if (requiredViewLine != thisRange.viewLine) {
01603 thisRange = range(realLine, requiredViewLine);
01604 }
01605 }
01606
01607 KateTextCursor ret(virtualLine, thisRange.startCol);
01608
01609
01610 if (keepX) {
01611 ret.setCol(thisRange.endCol - 1);
01612 KateTextCursor realCursorTemp(m_doc->getRealLine(virtualCursor.line()), virtualCursor.col());
01613 int visibleX = m_view->renderer()->textWidth(realCursorTemp) - range(realCursorTemp).startX;
01614 int xOffset = thisRange.startX;
01615
01616 if (m_currentMaxX > visibleX)
01617 visibleX = m_currentMaxX;
01618
01619 cXPos = xOffset + visibleX;
01620
01621 cXPos = kMin(cXPos, lineMaxCursorX(thisRange));
01622
01623 m_view->renderer()->textWidth(ret, cXPos);
01624 }
01625
01626 return ret;
01627 }
01628
01629 currentOffset++;
01630
01631 } while (thisRange.wrap);
01632
01633 if (forwards)
01634 virtualLine++;
01635 else
01636 virtualLine--;
01637 }
01638
01639
01640
01641 if (forwards)
01642 return KateTextCursor(m_doc->visibleLines() - 1, m_doc->lineLength(m_doc->visibleLines() - 1));
01643 else
01644 return KateTextCursor(0, 0);
01645 }
01646
01647 int KateViewInternal::lineMaxCursorX(const KateLineRange& range)
01648 {
01649 if (!m_view->wrapCursor() && !range.wrap)
01650 return INT_MAX;
01651
01652 int maxX = range.endX;
01653
01654 if (maxX && range.wrap) {
01655 QChar lastCharInLine = textLine(range.line)->getChar(range.endCol - 1);
01656 maxX -= m_view->renderer()->config()->fontMetrics()->width(lastCharInLine);
01657 }
01658
01659 return maxX;
01660 }
01661
01662 int KateViewInternal::lineMaxCol(const KateLineRange& range)
01663 {
01664 int maxCol = range.endCol;
01665
01666 if (maxCol && range.wrap)
01667 maxCol--;
01668
01669 return maxCol;
01670 }
01671
01672 void KateViewInternal::cursorUp(bool sel)
01673 {
01674 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01675 QKeyEvent e(QEvent::KeyPress, Qt::Key_Up, 0, 0);
01676 m_view->m_codeCompletion->handleKey(&e);
01677 return;
01678 }
01679
01680 if (displayCursor.line() == 0 && (!m_view->dynWordWrap() || viewLine(cursor) == 0))
01681 return;
01682
01683 int newLine = cursor.line(), newCol = 0, xOffset = 0, startCol = 0;
01684 m_preserveMaxX = true;
01685
01686 if (m_view->dynWordWrap()) {
01687
01688 KateLineRange thisRange = currentRange();
01689
01690 KateLineRange pRange = previousRange();
01691
01692
01693 Q_ASSERT((cursor.line() == thisRange.line) &&
01694 (cursor.col() >= thisRange.startCol) &&
01695 (!thisRange.wrap || cursor.col() < thisRange.endCol));
01696
01697
01698 int visibleX = m_view->renderer()->textWidth(cursor) - thisRange.startX;
01699 int currentLineVisibleX = visibleX;
01700
01701
01702 visibleX += thisRange.xOffset();
01703 visibleX -= pRange.xOffset();
01704
01705
01706 visibleX = kMax(0, visibleX);
01707
01708 startCol = pRange.startCol;
01709 xOffset = pRange.startX;
01710 newLine = pRange.line;
01711
01712
01713
01714 if (thisRange.xOffset() && !pRange.xOffset() && currentLineVisibleX == 0)
01715 visibleX = m_currentMaxX;
01716 else if (visibleX < m_currentMaxX - pRange.xOffset())
01717 visibleX = m_currentMaxX - pRange.xOffset();
01718
01719 cXPos = xOffset + visibleX;
01720
01721 cXPos = kMin(cXPos, lineMaxCursorX(pRange));
01722
01723 newCol = kMin((int)m_view->renderer()->textPos(newLine, visibleX, startCol), lineMaxCol(pRange));
01724
01725 } else {
01726 newLine = m_doc->getRealLine(displayCursor.line() - 1);
01727
01728 if ((m_view->wrapCursor()) && m_currentMaxX > cXPos)
01729 cXPos = m_currentMaxX;
01730 }
01731
01732 KateTextCursor c(newLine, newCol);
01733 m_view->renderer()->textWidth(c, cXPos);
01734
01735 updateSelection( c, sel );
01736 updateCursor( c );
01737 }
01738
01739 void KateViewInternal::cursorDown(bool sel)
01740 {
01741 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01742 QKeyEvent e(QEvent::KeyPress, Qt::Key_Down, 0, 0);
01743 m_view->m_codeCompletion->handleKey(&e);
01744 return;
01745 }
01746
01747 if ((displayCursor.line() >= (int)m_doc->numVisLines() - 1) && (!m_view->dynWordWrap() || viewLine(cursor) == lastViewLine(cursor.line())))
01748 return;
01749
01750 int newLine = cursor.line(), newCol = 0, xOffset = 0, startCol = 0;
01751 m_preserveMaxX = true;
01752
01753 if (m_view->dynWordWrap()) {
01754
01755 KateLineRange thisRange = currentRange();
01756
01757 KateLineRange nRange = nextRange();
01758
01759
01760 Q_ASSERT((cursor.line() == thisRange.line) &&
01761 (cursor.col() >= thisRange.startCol) &&
01762 (!thisRange.wrap || cursor.col() < thisRange.endCol));
01763
01764
01765 int visibleX = m_view->renderer()->textWidth(cursor) - thisRange.startX;
01766 int currentLineVisibleX = visibleX;
01767
01768
01769 visibleX += thisRange.xOffset();
01770 visibleX -= nRange.xOffset();
01771
01772
01773 visibleX = kMax(0, visibleX);
01774
01775 if (!thisRange.wrap) {
01776 newLine = m_doc->getRealLine(displayCursor.line() + 1);
01777 } else {
01778 startCol = thisRange.endCol;
01779 xOffset = thisRange.endX;
01780 }
01781
01782
01783
01784 if (thisRange.xOffset() && !nRange.xOffset() && currentLineVisibleX == 0)
01785 visibleX = m_currentMaxX;
01786 else if (visibleX < m_currentMaxX - nRange.xOffset())
01787 visibleX = m_currentMaxX - nRange.xOffset();
01788
01789 cXPos = xOffset + visibleX;
01790
01791 cXPos = kMin(cXPos, lineMaxCursorX(nRange));
01792
01793 newCol = kMin((int)m_view->renderer()->textPos(newLine, visibleX, startCol), lineMaxCol(nRange));
01794
01795 } else {
01796 newLine = m_doc->getRealLine(displayCursor.line() + 1);
01797
01798 if ((m_view->wrapCursor()) && m_currentMaxX > cXPos)
01799 cXPos = m_currentMaxX;
01800 }
01801
01802 KateTextCursor c(newLine, newCol);
01803 m_view->renderer()->textWidth(c, cXPos);
01804
01805 updateSelection(c, sel);
01806 updateCursor(c);
01807 }
01808
01809 void KateViewInternal::cursorToMatchingBracket( bool sel )
01810 {
01811 KateTextCursor start( cursor ), end;
01812
01813 if( !m_doc->findMatchingBracket( start, end ) )
01814 return;
01815
01816
01817
01818
01819 if( end > start )
01820 end.setCol(end.col() + 1);
01821
01822 updateSelection( end, sel );
01823 updateCursor( end );
01824 }
01825
01826 void KateViewInternal::topOfView( bool sel )
01827 {
01828 KateTextCursor c = viewLineOffset(startPos(), m_minLinesVisible);
01829 updateSelection( c, sel );
01830 updateCursor( c );
01831 }
01832
01833 void KateViewInternal::bottomOfView( bool sel )
01834 {
01835
01836 KateTextCursor c = viewLineOffset(endPos(), -m_minLinesVisible);
01837 updateSelection( c, sel );
01838 updateCursor( c );
01839 }
01840
01841
01842 void KateViewInternal::scrollLines( int lines, bool sel )
01843 {
01844 KateTextCursor c = viewLineOffset(displayCursor, lines, true);
01845
01846
01847 c.setLine(m_doc->getRealLine(c.line()));
01848
01849 updateSelection( c, sel );
01850 updateCursor( c );
01851 }
01852
01853
01854 void KateViewInternal::scrollUp()
01855 {
01856 KateTextCursor newPos = viewLineOffset(m_startPos, -1);
01857 scrollPos(newPos);
01858 }
01859
01860 void KateViewInternal::scrollDown()
01861 {
01862 KateTextCursor newPos = viewLineOffset(m_startPos, 1);
01863 scrollPos(newPos);
01864 }
01865
01866 void KateViewInternal::setAutoCenterLines(int viewLines, bool updateView)
01867 {
01868 m_autoCenterLines = viewLines;
01869 m_minLinesVisible = kMin(int((linesDisplayed() - 1)/2), m_autoCenterLines);
01870 if (updateView)
01871 KateViewInternal::updateView();
01872 }
01873
01874 void KateViewInternal::pageUp( bool sel )
01875 {
01876 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01877 QKeyEvent e(QEvent::KeyPress, Qt::Key_PageUp, 0, 0);
01878 m_view->m_codeCompletion->handleKey(&e);
01879 return;
01880 }
01881
01882
01883 int viewLine = displayViewLine(displayCursor);
01884 bool atTop = (startPos().line() == 0 && startPos().col() == 0);
01885
01886
01887 int lineadj = 2 * m_minLinesVisible;
01888 int cursorStart = (linesDisplayed() - 1) - viewLine;
01889 if (cursorStart < m_minLinesVisible)
01890 lineadj -= m_minLinesVisible - cursorStart;
01891
01892 int linesToScroll = -kMax( ((int)linesDisplayed() - 1) - lineadj, 0 );
01893 m_preserveMaxX = true;
01894
01895 if (!m_doc->pageUpDownMovesCursor () && !atTop) {
01896 int xPos = m_view->renderer()->textWidth(cursor) - currentRange().startX;
01897
01898 KateTextCursor newStartPos = viewLineOffset(startPos(), linesToScroll - 1);
01899 scrollPos(newStartPos);
01900
01901
01902 KateTextCursor newPos = viewLineOffset(newStartPos, viewLine, true);
01903 newPos.setLine(m_doc->getRealLine(newPos.line()));
01904
01905 KateLineRange newLine = range(newPos);
01906
01907 if (m_currentMaxX - newLine.xOffset() > xPos)
01908 xPos = m_currentMaxX - newLine.xOffset();
01909
01910 cXPos = kMin(newLine.startX + xPos, lineMaxCursorX(newLine));
01911
01912 m_view->renderer()->textWidth( newPos, cXPos );
01913
01914 m_preserveMaxX = true;
01915 updateSelection( newPos, sel );
01916 updateCursor(newPos);
01917
01918 } else {
01919 scrollLines( linesToScroll, sel );
01920 }
01921 }
01922
01923 void KateViewInternal::pageDown( bool sel )
01924 {
01925 if (m_view->m_codeCompletion->codeCompletionVisible()) {
01926 QKeyEvent e(QEvent::KeyPress, Qt::Key_PageDown, 0, 0);
01927 m_view->m_codeCompletion->handleKey(&e);
01928 return;
01929 }
01930
01931
01932 int viewLine = displayViewLine(displayCursor);
01933 bool atEnd = startPos() >= m_cachedMaxStartPos;
01934
01935
01936 int lineadj = 2 * m_minLinesVisible;
01937 int cursorStart = m_minLinesVisible - viewLine;
01938 if (cursorStart > 0)
01939 lineadj -= cursorStart;
01940
01941 int linesToScroll = kMax( ((int)linesDisplayed() - 1) - lineadj, 0 );
01942 m_preserveMaxX = true;
01943
01944 if (!m_doc->pageUpDownMovesCursor () && !atEnd) {
01945 int xPos = m_view->renderer()->textWidth(cursor) - currentRange().startX;
01946
01947 KateTextCursor newStartPos = viewLineOffset(startPos(), linesToScroll + 1);
01948 scrollPos(newStartPos);
01949
01950
01951 KateTextCursor newPos = viewLineOffset(newStartPos, viewLine, true);
01952 newPos.setLine(m_doc->getRealLine(newPos.line()));
01953
01954 KateLineRange newLine = range(newPos);
01955
01956 if (m_currentMaxX - newLine.xOffset() > xPos)
01957 xPos = m_currentMaxX - newLine.xOffset();
01958
01959 cXPos = kMin(newLine.startX + xPos, lineMaxCursorX(newLine));
01960
01961 m_view->renderer()->textWidth( newPos, cXPos );
01962
01963 m_preserveMaxX = true;
01964 updateSelection( newPos, sel );
01965 updateCursor(newPos);
01966
01967 } else {
01968 scrollLines( linesToScroll, sel );
01969 }
01970 }
01971
01972 int KateViewInternal::maxLen(uint startLine)
01973 {
01974
01975
01976 int displayLines = (m_view->height() / m_view->renderer()->fontHeight()) + 1;
01977
01978 int maxLen = 0;
01979
01980 for (int z = 0; z < displayLines; z++) {
01981 int virtualLine = startLine + z;
01982
01983 if (virtualLine < 0 || virtualLine >= (int)m_doc->visibleLines())
01984 break;
01985
01986 KateLineRange thisRange = range((int)m_doc->getRealLine(virtualLine));
01987
01988 maxLen = kMax(maxLen, thisRange.endX);
01989 }
01990
01991 return maxLen;
01992 }
01993
01994 void KateViewInternal::top( bool sel )
01995 {
01996 KateTextCursor c( 0, cursor.col() );
01997 m_view->renderer()->textWidth( c, cXPos );
01998 updateSelection( c, sel );
01999 updateCursor( c );
02000 }
02001
02002 void KateViewInternal::bottom( bool sel )
02003 {
02004 KateTextCursor c( m_doc->lastLine(), cursor.col() );
02005 m_view->renderer()->textWidth( c, cXPos );
02006 updateSelection( c, sel );
02007 updateCursor( c );
02008 }
02009
02010 void KateViewInternal::top_home( bool sel )
02011 {
02012 if (m_view->m_codeCompletion->codeCompletionVisible()) {
02013 QKeyEvent e(QEvent::KeyPress, Qt::Key_Home, 0, 0);
02014 m_view->m_codeCompletion->handleKey(&e);
02015 return;
02016 }
02017 KateTextCursor c( 0, 0 );
02018 updateSelection( c, sel );
02019 updateCursor( c );
02020 }
02021
02022 void KateViewInternal::bottom_end( bool sel )
02023 {
02024 if (m_view->m_codeCompletion->codeCompletionVisible()) {
02025 QKeyEvent e(QEvent::KeyPress, Qt::Key_End, 0, 0);
02026 m_view->m_codeCompletion->handleKey(&e);
02027 return;
02028 }
02029 KateTextCursor c( m_doc->lastLine(), m_doc->lineLength( m_doc->lastLine() ) );
02030 updateSelection( c, sel );
02031 updateCursor( c );
02032 }
02033
02034 void KateViewInternal::updateSelection( const KateTextCursor& _newCursor, bool keepSel )
02035 {
02036 KateTextCursor newCursor = _newCursor;
02037 if( keepSel )
02038 {
02039 if ( !m_view->hasSelection() || (selectAnchor.line() == -1)
02040 || (m_view->config()->persistentSelection()
02041 && ((cursor < m_view->selectStart) || (cursor > m_view->selectEnd))) )
02042 {
02043 selectAnchor = cursor;
02044 m_view->setSelection( cursor, newCursor );
02045 }
02046 else
02047 {
02048 bool doSelect = true;
02049 switch (m_selectionMode)
02050 {
02051 case Word:
02052 {
02053
02054
02055
02056
02057
02058
02059
02060 if ( selStartCached.line() == -1 )
02061 selStartCached = selEndCached;
02062
02063 int c;
02064 if ( newCursor > selEndCached )
02065 {
02066 selectAnchor = selStartCached;
02067
02068 KateTextLine::Ptr l = m_doc->kateTextLine( newCursor.line() );
02069
02070 c = newCursor.col();
02071 if ( c > 0 && m_doc->highlight()->isInWord( l->getChar( c-1 ) ) ) {
02072 for (; c < l->length(); c++ )
02073 if ( !m_doc->highlight()->isInWord( l->getChar( c ) ) )
02074 break;
02075 }
02076
02077 newCursor.setCol( c );
02078 }
02079 else if ( newCursor < selStartCached )
02080 {
02081 selectAnchor = selEndCached;
02082
02083 KateTextLine::Ptr l = m_doc->kateTextLine( newCursor.line() );
02084
02085 c = newCursor.col();
02086 if ( c > 0 && c < m_doc->textLine( newCursor.line() ).length()
02087 && m_doc->highlight()->isInWord( l->getChar( c ) )
02088 && m_doc->highlight()->isInWord( l->getChar( c-1 ) ) ) {
02089 for ( c -= 2; c >= 0; c-- )
02090 if ( !m_doc->highlight()->isInWord( l->getChar( c ) ) )
02091 break;
02092 newCursor.setCol( c+1 );
02093 }
02094
02095 }
02096 else
02097 doSelect = false;
02098
02099 }
02100 break;
02101 case Line:
02102 if ( newCursor.line() > selStartCached.line() )
02103 {
02104 if ( newCursor.line()+1 >= m_doc->numLines() )
02105 newCursor.setCol( m_doc->textLine( newCursor.line() ).length() );
02106 else
02107 newCursor.setPos( newCursor.line() + 1, 0 );
02108
02109 selectAnchor = selStartCached;
02110 selectAnchor.setCol( 0 );
02111 }
02112 else if ( newCursor.line() < selStartCached.line() )
02113 {
02114 newCursor.setCol( 0 );
02115
02116 selectAnchor = selEndCached;
02117 if ( selectAnchor.col() > 0 )
02118 {
02119 if ( selectAnchor.line()+1 >= m_doc->numLines() )
02120 selectAnchor.setCol( m_doc->textLine( selectAnchor.line() ).length() );
02121 else
02122 selectAnchor.setPos( selectAnchor.line() + 1, 0 );
02123 }
02124 }
02125 else
02126 doSelect = false;
02127 break;
02128 case Mouse:
02129 {
02130 if ( selStartCached.line() < 0 )
02131 break;
02132
02133 if ( newCursor > selEndCached )
02134 selectAnchor = selStartCached;
02135 else if ( newCursor < selStartCached )
02136 selectAnchor = selEndCached;
02137 else
02138 doSelect = false;
02139 }
02140 break;
02141 default:
02142 {
02143 if ( selectAnchor.line() < 0 )
02144 break;
02145 }
02146 }
02147
02148 if ( doSelect )
02149 m_view->setSelection( selectAnchor, newCursor);
02150 else if ( selStartCached.line() >= 0 )
02151 m_view->setSelection( selStartCached, selEndCached );
02152 }
02153
02154 m_selChangedByUser = true;
02155 }
02156 else if ( !m_view->config()->persistentSelection() )
02157 {
02158 m_view->clearSelection();
02159 selStartCached.setLine( -1 );
02160 selectAnchor.setLine( -1 );
02161 }
02162 }
02163
02164 void KateViewInternal::updateCursor( const KateTextCursor& newCursor, bool force, bool center, bool calledExternally )
02165 {
02166 if ( !force && (cursor == newCursor) )
02167 {
02168 if ( !m_madeVisible && m_view == m_doc->activeView() )
02169 {
02170
02171 m_doc->foldingTree()->ensureVisible( newCursor.line() );
02172
02173 makeVisible ( displayCursor, displayCursor.col(), false, center, calledExternally );
02174 }
02175
02176 return;
02177 }
02178
02179
02180 m_doc->foldingTree()->ensureVisible( newCursor.line() );
02181
02182 KateTextCursor oldDisplayCursor = displayCursor;
02183
02184 cursor.setPos (newCursor);
02185 displayCursor.setPos (m_doc->getVirtualLine(cursor.line()), cursor.col());
02186
02187 cXPos = m_view->renderer()->textWidth( cursor );
02188 if (m_view == m_doc->activeView())
02189 makeVisible ( displayCursor, displayCursor.col(), false, center, calledExternally );
02190
02191 updateBracketMarks();
02192
02193
02194 tagLine(oldDisplayCursor);
02195 tagLine(displayCursor);
02196
02197 updateMicroFocusHint();
02198
02199 if (m_cursorTimer.isActive ())
02200 {
02201 if ( KApplication::cursorFlashTime() > 0 )
02202 m_cursorTimer.start( KApplication::cursorFlashTime() / 2 );
02203 m_view->renderer()->setDrawCaret(true);
02204 }
02205
02206
02207 if (m_preserveMaxX)
02208 m_preserveMaxX = false;
02209 else
02210 if (m_view->dynWordWrap())
02211 m_currentMaxX = m_view->renderer()->textWidth(displayCursor) - currentRange().startX + currentRange().xOffset();
02212 else
02213 m_currentMaxX = cXPos;
02214
02215
02216
02217
02218 paintText(0, 0, width(), height(), true);
02219
02220 emit m_view->cursorPositionChanged();
02221 }
02222
02223 void KateViewInternal::updateBracketMarks()
02224 {
02225 if ( bm.isValid() ) {
02226 KateTextCursor bmStart(m_doc->getVirtualLine(bm.start().line()), bm.start().col());
02227 KateTextCursor bmEnd(m_doc->getVirtualLine(bm.end().line()), bm.end().col());
02228
02229 if( bm.getMinIndent() != 0 )
02230 {
02231
02232 if( bmStart > bmEnd )
02233 {
02234 tagLines(bmEnd, bmStart, true);
02235 }
02236 else
02237 {
02238 tagLines(bmStart, bmEnd, true);
02239 }
02240 }
02241 else
02242 {
02243 tagLine(bmStart);
02244 tagLine(bmEnd);
02245 }
02246 }
02247
02248
02249 int maxLines = linesDisplayed () * 3;
02250 m_doc->newBracketMark( cursor, bm, maxLines );
02251
02252 if ( bm.isValid() ) {
02253 KateTextCursor bmStart(m_doc->getVirtualLine(bm.start().line()), bm.start().col());
02254 KateTextCursor bmEnd(m_doc->getVirtualLine(bm.end().line()), bm.end().col());
02255
02256 if( bm.getMinIndent() != 0 )
02257 {
02258
02259 if( bmStart > bmEnd )
02260 {
02261 tagLines(bmEnd, bmStart, true);
02262 }
02263 else
02264 {
02265 tagLines(bmStart, bmEnd, true);
02266 }
02267 }
02268 else
02269 {
02270 tagLine(bmStart);
02271 tagLine(bmEnd);
02272 }
02273 }
02274 }
02275
02276 bool KateViewInternal::tagLine(const KateTextCursor& virtualCursor)
02277 {
02278 int viewLine = displayViewLine(virtualCursor, true);
02279 if (viewLine >= 0 && viewLine < (int)lineRanges.count()) {
02280 lineRanges[viewLine].dirty = true;
02281 leftBorder->update (0, lineToY(viewLine), leftBorder->width(), m_view->renderer()->fontHeight());
02282 return true;
02283 }
02284 return false;
02285 }
02286
02287 bool KateViewInternal::tagLines( int start, int end, bool realLines )
02288 {
02289 return tagLines(KateTextCursor(start, 0), KateTextCursor(end, -1), realLines);
02290 }
02291
02292 bool KateViewInternal::tagLines(KateTextCursor start, KateTextCursor end, bool realCursors)
02293 {
02294 if (realCursors)
02295 {
02296
02297 start.setLine(m_doc->getVirtualLine( start.line() ));
02298 end.setLine(m_doc->getVirtualLine( end.line() ));
02299 }
02300
02301 if (end.line() < (int)startLine())
02302 {
02303
02304 return false;
02305 }
02306 if (start.line() > (int)endLine())
02307 {
02308
02309 return false;
02310 }
02311
02312
02313
02314 bool ret = false;
02315
02316 for (uint z = 0; z < lineRanges.size(); z++)
02317 {
02318 if ((lineRanges[z].virtualLine > start.line() || (lineRanges[z].virtualLine == start.line() && lineRanges[z].endCol >= start.col() && start.col() != -1)) && (lineRanges[z].virtualLine < end.line() || (lineRanges[z].virtualLine == end.line() && (lineRanges[z].startCol <= end.col() || end.col() == -1)))) {
02319 ret = lineRanges[z].dirty = true;
02320
02321 }
02322 }
02323
02324 if (!m_view->dynWordWrap())
02325 {
02326 int y = lineToY( start.line() );
02327
02328 int h = (end.line() - start.line() + 2) * m_view->renderer()->fontHeight();
02329 if (end.line() == (int)m_doc->numVisLines() - 1)
02330 h = height();
02331
02332 leftBorder->update (0, y, leftBorder->width(), h);
02333 }
02334 else
02335 {
02336
02337
02338 for (uint z = 0; z < lineRanges.size(); z++)
02339 {
02340 if ((lineRanges[z].virtualLine > start.line() || (lineRanges[z].virtualLine == start.line() && lineRanges[z].endCol >= start.col() && start.col() != -1)) && (lineRanges[z].virtualLine < end.line() || (lineRanges[z].virtualLine == end.line() && (lineRanges[z].startCol <= end.col() || end.col() == -1))))
02341 {
02342
02343 leftBorder->update (0, z * m_view->renderer()->fontHeight(), leftBorder->width(), leftBorder->height());
02344 break;
02345 }
02346
02347
02348
02349
02350
02351
02352 }
02353 }
02354
02355 return ret;
02356 }
02357
02358 void KateViewInternal::tagAll()
02359 {
02360
02361 for (uint z = 0; z < lineRanges.size(); z++)
02362 {
02363 lineRanges[z].dirty = true;
02364 }
02365
02366 leftBorder->updateFont();
02367 leftBorder->update ();
02368 }
02369
02370 void KateViewInternal::paintCursor()
02371 {
02372 if (tagLine(displayCursor))
02373 paintText (0,0,width(), height(), true);
02374 }
02375
02376
02377 void KateViewInternal::placeCursor( const QPoint& p, bool keepSelection, bool updateSelection )
02378 {
02379 KateLineRange thisRange = yToKateLineRange(p.y());
02380
02381 if (thisRange.line == -1) {
02382 for (int i = (p.y() / m_view->renderer()->fontHeight()); i >= 0; i--) {
02383 thisRange = lineRanges[i];
02384 if (thisRange.line != -1)
02385 break;
02386 }
02387 Q_ASSERT(thisRange.line != -1);
02388 }
02389
02390 int realLine = thisRange.line;
02391 int visibleLine = thisRange.virtualLine;
02392 uint startCol = thisRange.startCol;
02393
02394 visibleLine = kMax( 0, kMin( visibleLine, int(m_doc->numVisLines()) - 1 ) );
02395
02396 KateTextCursor c(realLine, 0);
02397
02398 int x = kMin(kMax(-m_startX, p.x() - thisRange.xOffset()), lineMaxCursorX(thisRange) - thisRange.startX);
02399
02400 m_view->renderer()->textWidth( c, startX() + x, startCol);
02401
02402 if (updateSelection)
02403 KateViewInternal::updateSelection( c, keepSelection );
02404
02405 updateCursor( c );
02406 }
02407
02408
02409 bool KateViewInternal::isTargetSelected( const QPoint& p )
02410 {
02411 KateLineRange thisRange = yToKateLineRange(p.y());
02412
02413 KateTextLine::Ptr l = textLine( thisRange.line );
02414 if( !l )
02415 return false;
02416
02417 int col = m_view->renderer()->textPos( l, startX() + p.x() - thisRange.xOffset(), thisRange.startCol, false );
02418
02419 return m_view->lineColSelected( thisRange.line, col );
02420 }
02421
02422
02423
02424 bool KateViewInternal::eventFilter( QObject *obj, QEvent *e )
02425 {
02426 if (obj == m_lineScroll)
02427 {
02428
02429 if (e->type() == QEvent::Wheel && m_lineScroll->minValue() != m_lineScroll->maxValue())
02430 {
02431 wheelEvent((QWheelEvent*)e);
02432 return true;
02433 }
02434
02435
02436 return QWidget::eventFilter( obj, e );
02437 }
02438
02439 switch( e->type() )
02440 {
02441 case QEvent::KeyPress:
02442 {
02443 QKeyEvent *k = (QKeyEvent *)e;
02444
02445 if (m_view->m_codeCompletion->codeCompletionVisible ())
02446 {
02447 kdDebug (13030) << "hint around" << endl;
02448
02449 if( k->key() == Key_Escape )
02450 m_view->m_codeCompletion->abortCompletion();
02451 }
02452
02453 if ((k->key() == Qt::Key_Escape) && !m_view->config()->persistentSelection() )
02454 {
02455 m_view->clearSelection();
02456 return true;
02457 }
02458 else if ( !((k->state() & ControlButton) || (k->state() & AltButton)) )
02459 {
02460 keyPressEvent( k );
02461 return k->isAccepted();
02462 }
02463
02464 } break;
02465
02466 case QEvent::DragMove:
02467 {
02468 QPoint currentPoint = ((QDragMoveEvent*) e)->pos();
02469
02470 QRect doNotScrollRegion( scrollMargin, scrollMargin,
02471 width() - scrollMargin * 2,
02472 height() - scrollMargin * 2 );
02473
02474 if ( !doNotScrollRegion.contains( currentPoint ) )
02475 {
02476 startDragScroll();
02477
02478 ( (QDragMoveEvent*)e )->accept( QRect(0,0,0,0) );
02479 }
02480
02481 dragMoveEvent((QDragMoveEvent*)e);
02482 } break;
02483
02484 case QEvent::DragLeave:
02485
02486 stopDragScroll();
02487 break;
02488
02489 case QEvent::WindowBlocked:
02490
02491
02492 m_doc->m_isasking = -1;
02493 break;
02494
02495 default:
02496 break;
02497 }
02498
02499 return QWidget::eventFilter( obj, e );
02500 }
02501
02502 void KateViewInternal::keyPressEvent( QKeyEvent* e )
02503 {
02504 KKey key(e);
02505
02506 bool codeComp = m_view->m_codeCompletion->codeCompletionVisible ();
02507
02508 if (codeComp)
02509 {
02510 kdDebug (13030) << "hint around" << endl;
02511
02512 if( e->key() == Key_Enter || e->key() == Key_Return ||
02513 (key == SHIFT + Qt::Key_Return) || (key == SHIFT + Qt::Key_Enter)) {
02514 m_view->m_codeCompletion->doComplete();
02515 e->accept();
02516 return;
02517 }
02518 }
02519
02520 if( !m_doc->isReadWrite() )
02521 {
02522 e->ignore();
02523 return;
02524 }
02525
02526 if ((key == Qt::Key_Return) || (key == Qt::Key_Enter))
02527 {
02528 m_view->keyReturn();
02529 e->accept();
02530 return;
02531 }
02532
02533 if ((key == SHIFT + Qt::Key_Return) || (key == SHIFT + Qt::Key_Enter))
02534 {
02535 uint ln = cursor.line();
02536 int col = cursor.col();
02537 KateTextLine::Ptr line = m_doc->kateTextLine( ln );
02538 int pos = line->firstChar();
02539 if (pos > cursor.col()) pos = cursor.col();
02540 if (pos != -1) {
02541 while ((int)line->length() > pos &&
02542 !line->getChar(pos).isLetterOrNumber() &&
02543 pos < cursor.col()) ++pos;
02544 } else {
02545 pos = line->length();
02546 }
02547 m_doc->editStart();
02548 m_doc->insertText( cursor.line(), line->length(), "\n" + line->string(0, pos)
02549 + line->string().right( line->length() - cursor.col() ) );
02550 cursor.setPos(ln + 1, pos);
02551 if (col < int(line->length()))
02552 m_doc->editRemoveText(ln, col, line->length() - col);
02553 m_doc->editEnd();
02554 updateCursor(cursor, true);
02555 updateView();
02556 e->accept();
02557
02558 return;
02559 }
02560
02561 if (key == Qt::Key_Backspace || key == SHIFT + Qt::Key_Backspace)
02562 {
02563 m_view->backspace();
02564 e->accept();
02565
02566 if (codeComp)
02567 m_view->m_codeCompletion->updateBox ();
02568
02569 return;
02570 }
02571
02572 if (key == Qt::Key_Tab || key == SHIFT+Qt::Key_Backtab || key == Qt::Key_Backtab)
02573 {
02574 if (m_doc->invokeTabInterceptor(key)) {
02575 e->accept();
02576 return;
02577 } else
02578 if (m_doc->configFlags() & KateDocumentConfig::cfTabIndents)
02579 {
02580 if( key == Qt::Key_Tab )
02581 {
02582 if (m_view->hasSelection() || (m_doc->configFlags() & KateDocumentConfig::cfTabIndentsMode))
02583 m_doc->indent( m_view, cursor.line(), 1 );
02584 else if (m_doc->configFlags() & KateDocumentConfig::cfTabInsertsTab)
02585 m_doc->typeChars ( m_view, QString ("\t") );
02586 else
02587 m_doc->insertIndentChars ( m_view );
02588
02589 e->accept();
02590
02591 if (codeComp)
02592 m_view->m_codeCompletion->updateBox ();
02593
02594 return;
02595 }
02596
02597 if (key == SHIFT+Qt::Key_Backtab || key == Qt::Key_Backtab)
02598 {
02599 m_doc->indent( m_view, cursor.line(), -1 );
02600 e->accept();
02601
02602 if (codeComp)
02603 m_view->m_codeCompletion->updateBox ();
02604
02605 return;
02606 }
02607 }
02608 }
02609 if ( !(e->state() & ControlButton) && !(e->state() & AltButton)
02610 && m_doc->typeChars ( m_view, e->text() ) )
02611 {
02612 e->accept();
02613
02614 if (codeComp)
02615 m_view->m_codeCompletion->updateBox ();
02616
02617 return;
02618 }
02619
02620 e->ignore();
02621 }
02622
02623 void KateViewInternal::keyReleaseEvent( QKeyEvent* e )
02624 {
02625 KKey key(e);
02626
02627 if (key == SHIFT)
02628 m_shiftKeyPressed = true;
02629 else
02630 {
02631 if (m_shiftKeyPressed)
02632 {
02633 m_shiftKeyPressed = false;
02634
02635 if (m_selChangedByUser)
02636 {
02637 QApplication::clipboard()->setSelectionMode( true );
02638 m_view->copy();
02639 QApplication::clipboard()->setSelectionMode( false );
02640
02641 m_selChangedByUser = false;
02642 }
02643 }
02644 }
02645
02646 e->ignore();
02647 return;
02648 }
02649
02650 void KateViewInternal::contextMenuEvent ( QContextMenuEvent * e )
02651 {
02652
02653
02654 QPoint p = e->pos();
02655
02656 if ( m_view->m_doc->browserView() )
02657 {
02658 m_view->contextMenuEvent( e );
02659 return;
02660 }
02661
02662 if ( e->reason() == QContextMenuEvent::Keyboard )
02663 {
02664 makeVisible( cursor, 0 );
02665 p = cursorCoordinates();
02666 }
02667 else if ( ! m_view->hasSelection() || m_view->config()->persistentSelection() )
02668 placeCursor( e->pos() );
02669
02670
02671 if (m_view->popup()) {
02672 m_view->popup()->popup( mapToGlobal( p ) );
02673 e->accept ();
02674 }
02675 }
02676
02677 void KateViewInternal::mousePressEvent( QMouseEvent* e )
02678 {
02679 switch (e->button())
02680 {
02681 case LeftButton:
02682 m_selChangedByUser = false;
02683
02684 if (possibleTripleClick)
02685 {
02686 possibleTripleClick = false;
02687
02688 m_selectionMode = Line;
02689
02690 if ( e->state() & Qt::ShiftButton )
02691 {
02692 updateSelection( cursor, true );
02693 }
02694 else
02695 {
02696 m_view->selectLine( cursor );
02697 }
02698
02699 QApplication::clipboard()->setSelectionMode( true );
02700 m_view->copy();
02701 QApplication::clipboard()->setSelectionMode( false );
02702
02703
02704
02705 if ( selectAnchor.line() > m_view->selectStart.line() )
02706 {
02707
02708 if ( selectAnchor == m_view->selectEnd && selectAnchor.col() == 0 )
02709 selStartCached = KateTextCursor( selectAnchor.line()-1, 0 );
02710 else
02711 selStartCached = KateTextCursor( selectAnchor.line(), 0 );
02712 selEndCached = m_view->selectEnd;
02713 }
02714 else
02715 {
02716
02717 selStartCached = m_view->selectStart;
02718 if ( m_view->selectEnd.line() > m_view->selectStart.line() )
02719 selEndCached = KateTextCursor( m_view->selectStart.line()+1, 0 );
02720 else
02721 selEndCached = m_view->selectEnd;
02722 }
02723
02724
02725
02726 if ( m_view->selectStart < selectAnchor
02727 && selectAnchor.line() != m_view->selectStart.line() )
02728 updateCursor( m_view->selectStart );
02729 else
02730 updateCursor( m_view->selectEnd );
02731
02732 e->accept ();
02733 return;
02734 }
02735 else if (m_selectionMode == Default)
02736 {
02737 m_selectionMode = Mouse;
02738 }
02739
02740 if ( e->state() & Qt::ShiftButton )
02741 {
02742 if (selectAnchor.line() < 0)
02743 selectAnchor = cursor;
02744 }
02745 else
02746 {
02747 selStartCached.setLine( -1 );
02748 }
02749
02750 if( !( e->state() & Qt::ShiftButton ) && isTargetSelected( e->pos() ) )
02751 {
02752 dragInfo.state = diPending;
02753 dragInfo.start = e->pos();
02754 }
02755 else
02756 {
02757 dragInfo.state = diNone;
02758
02759 if ( e->state() & Qt::ShiftButton )
02760 {
02761 placeCursor( e->pos(), true, false );
02762 if ( selStartCached.line() >= 0 )
02763 {
02764 if ( cursor > selEndCached )
02765 {
02766 m_view->setSelection( selStartCached, cursor );
02767 selectAnchor = selStartCached;
02768 }
02769 else if ( cursor < selStartCached )
02770 {
02771 m_view->setSelection( cursor, selEndCached );
02772 selectAnchor = selEndCached;
02773 }
02774 else
02775 {
02776 m_view->setSelection( selStartCached, cursor );
02777 }
02778 }
02779 else
02780 {
02781 m_view->setSelection( selectAnchor, cursor );
02782 }
02783 }
02784 else
02785 {
02786 placeCursor( e->pos() );
02787 }
02788
02789 scrollX = 0;
02790 scrollY = 0;
02791
02792 m_scrollTimer.start (50);
02793 }
02794
02795 e->accept ();
02796 break;
02797
02798 default:
02799 e->ignore ();
02800 break;
02801 }
02802 }
02803
02804 void KateViewInternal::mouseDoubleClickEvent(QMouseEvent *e)
02805 {
02806 switch (e->button())
02807 {
02808 case LeftButton:
02809 m_selectionMode = Word;
02810
02811 if ( e->state() & Qt::ShiftButton )
02812 {
02813 KateTextCursor oldSelectStart = m_view->selectStart;
02814 KateTextCursor oldSelectEnd = m_view->selectEnd;
02815
02816
02817 int cs, ce;
02818 KateTextLine::Ptr l = m_doc->kateTextLine( selectAnchor.line() );
02819
02820 ce = selectAnchor.col();
02821 if ( ce > 0 && m_doc->highlight()->isInWord( l->getChar( ce ) ) ) {
02822 for (; ce < l->length(); ce++ )
02823 if ( !m_doc->highlight()->isInWord( l->getChar( ce ) ) )
02824 break;
02825 }
02826
02827 cs = selectAnchor.col() - 1;
02828 if ( cs < m_doc->textLine( selectAnchor.line() ).length()
02829 && m_doc->highlight()->isInWord( l->getChar( cs ) ) ) {
02830 for ( cs--; cs >= 0; cs-- )
02831 if ( !m_doc->highlight()->isInWord( l->getChar( cs ) ) )
02832 break;
02833 }
02834
02835
02836 if (cs+1 < ce)
02837 {
02838 selStartCached = KateTextCursor( selectAnchor.line(), cs+1 );
02839 selEndCached = KateTextCursor( selectAnchor.line(), ce );
02840 }
02841 else
02842 {
02843 selStartCached = selectAnchor;
02844 selEndCached = selectAnchor;
02845 }
02846
02847 placeCursor( e->pos(), true );
02848 }
02849 else
02850 {
02851
02852
02853
02854
02855
02856
02857
02858 m_view->clearSelection( false, false );
02859 placeCursor( e->pos() );
02860 m_view->selectWord( cursor );
02861 if (m_view->hasSelection())
02862 {
02863 selectAnchor = selStartCached = m_view->selectStart;
02864 selEndCached = m_view->selectEnd;
02865 }
02866 else
02867 {
02868
02869
02870 m_selectionMode = Default;
02871 }
02872 }
02873
02874
02875 if (m_view->hasSelection())
02876 {
02877 QApplication::clipboard()->setSelectionMode( true );
02878 m_view->copy();
02879 QApplication::clipboard()->setSelectionMode( false );
02880
02881
02882
02883 if (m_view->selectStart < selStartCached)
02884 updateCursor( m_view->selectStart );
02885 else
02886 updateCursor( m_view->selectEnd );
02887 }
02888
02889 possibleTripleClick = true;
02890 QTimer::singleShot ( QApplication::doubleClickInterval(), this, SLOT(tripleClickTimeout()) );
02891
02892 scrollX = 0;
02893 scrollY = 0;
02894
02895 m_scrollTimer.start (50);
02896
02897 e->accept ();
02898 break;
02899
02900 default:
02901 e->ignore ();
02902 break;
02903 }
02904 }
02905
02906 void KateViewInternal::tripleClickTimeout()
02907 {
02908 possibleTripleClick = false;
02909 }
02910
02911 void KateViewInternal::mouseReleaseEvent( QMouseEvent* e )
02912 {
02913 switch (e->button())
02914 {
02915 case LeftButton:
02916 m_selectionMode = Default;
02917
02918
02919 if (m_selChangedByUser)
02920 {
02921 QApplication::clipboard()->setSelectionMode( true );
02922 m_view->copy();
02923 QApplication::clipboard()->setSelectionMode( false );
02924
02925
02926 if ( m_view->selectStart < selectAnchor )
02927 updateCursor( m_view->selectStart );
02928 else
02929 updateCursor( m_view->selectEnd );
02930
02931 m_selChangedByUser = false;
02932 }
02933
02934 if (dragInfo.state == diPending)
02935 placeCursor( e->pos(), e->state() & ShiftButton );
02936 else if (dragInfo.state == diNone)
02937 m_scrollTimer.stop ();
02938
02939 dragInfo.state = diNone;
02940
02941 e->accept ();
02942 break;
02943
02944 case MidButton:
02945 placeCursor( e->pos() );
02946
02947 if( m_doc->isReadWrite() )
02948 {
02949 QApplication::clipboard()->setSelectionMode( true );
02950 m_view->paste ();
02951 QApplication::clipboard()->setSelectionMode( false );
02952 }
02953
02954 e->accept ();
02955 break;
02956
02957 default:
02958 e->ignore ();
02959 break;
02960 }
02961 }
02962
02963 void KateViewInternal::mouseMoveEvent( QMouseEvent* e )
02964 {
02965 if( e->state() & LeftButton )
02966 {
02967 if (dragInfo.state == diPending)
02968 {
02969
02970
02971 QPoint p( e->pos() - dragInfo.start );
02972
02973
02974 if( p.manhattanLength() > KGlobalSettings::dndEventDelay() )
02975 doDrag();
02976
02977 return;
02978 }
02979 else if (dragInfo.state == diDragging)
02980 {
02981
02982
02983 return;
02984 }
02985
02986 mouseX = e->x();
02987 mouseY = e->y();
02988
02989 scrollX = 0;
02990 scrollY = 0;
02991 int d = m_view->renderer()->fontHeight();
02992
02993 if (mouseX < 0)
02994 scrollX = -d;
02995
02996 if (mouseX > width())
02997 scrollX = d;
02998
02999 if (mouseY < 0)
03000 {
03001 mouseY = 0;
03002 scrollY = -d;
03003 }
03004
03005 if (mouseY > height())
03006 {
03007 mouseY = height();
03008 scrollY = d;
03009 }
03010
03011 placeCursor( QPoint( mouseX, mouseY ), true );
03012
03013 }
03014 else
03015 {
03016 if (isTargetSelected( e->pos() ) ) {
03017
03018
03019 if (m_mouseCursor != ArrowCursor) {
03020 setCursor( KCursor::arrowCursor() );
03021 m_mouseCursor = ArrowCursor;
03022 }
03023 } else {
03024
03025 if (m_mouseCursor != IbeamCursor) {
03026 setCursor( KCursor::ibeamCursor() );
03027 m_mouseCursor = IbeamCursor;
03028 }
03029 }
03030
03031 if (m_textHintEnabled)
03032 {
03033 m_textHintTimer.start(m_textHintTimeout);
03034 m_textHintMouseX=e->x();
03035 m_textHintMouseY=e->y();
03036 }
03037 }
03038 }
03039
03040 void KateViewInternal::paintEvent(QPaintEvent *e)
03041 {
03042 paintText(e->rect().x(), e->rect().y(), e->rect().width(), e->rect().height());
03043 }
03044
03045 void KateViewInternal::resizeEvent(QResizeEvent* e)
03046 {
03047 bool expandedHorizontally = width() > e->oldSize().width();
03048 bool expandedVertically = height() > e->oldSize().height();
03049 bool heightChanged = height() != e->oldSize().height();
03050
03051 m_madeVisible = false;
03052
03053 if (heightChanged) {
03054 setAutoCenterLines(m_autoCenterLines, false);
03055 m_cachedMaxStartPos.setPos(-1, -1);
03056 }
03057
03058 if (m_view->dynWordWrap()) {
03059 bool dirtied = false;
03060
03061 for (uint i = 0; i < lineRanges.count(); i++) {
03062
03063
03064 if (lineRanges[i].wrap ||
03065 (!expandedHorizontally && (lineRanges[i].endX - lineRanges[i].startX) > width())) {
03066 dirtied = lineRanges[i].dirty = true;
03067 break;
03068 }
03069 }
03070
03071 if (dirtied || heightChanged) {
03072 updateView(true);
03073 leftBorder->update();
03074 }
03075
03076 if (width() < e->oldSize().width()) {
03077 if (!m_view->wrapCursor()) {
03078
03079 if (cursor.col() > m_doc->lineLength(cursor.line())) {
03080 KateLineRange thisRange = currentRange();
03081
03082 KateTextCursor newCursor(cursor.line(), thisRange.endCol + ((width() - thisRange.xOffset() - (thisRange.endX - thisRange.startX)) / m_view->renderer()->spaceWidth()) - 1);
03083 updateCursor(newCursor);
03084 }
03085 }
03086 }
03087
03088 } else {
03089 updateView();
03090
03091 if (expandedHorizontally && startX() > 0)
03092 scrollColumns(startX() - (width() - e->oldSize().width()));
03093 }
03094
03095 if (expandedVertically) {
03096 KateTextCursor max = maxStartPos();
03097 if (startPos() > max)
03098 scrollPos(max);
03099 }
03100 }
03101
03102 void KateViewInternal::scrollTimeout ()
03103 {
03104 if (scrollX || scrollY)
03105 {
03106 scrollLines (startPos().line() + (scrollY / (int)m_view->renderer()->fontHeight()));
03107 placeCursor( QPoint( mouseX, mouseY ), true );
03108 }
03109 }
03110
03111 void KateViewInternal::cursorTimeout ()
03112 {
03113 m_view->renderer()->setDrawCaret(!m_view->renderer()->drawCaret());
03114 paintCursor();
03115 }
03116
03117 void KateViewInternal::textHintTimeout ()
03118 {
03119 m_textHintTimer.stop ();
03120
03121 KateLineRange thisRange = yToKateLineRange(m_textHintMouseY);
03122
03123 if (thisRange.line == -1) return;
03124
03125 if (m_textHintMouseX> (lineMaxCursorX(thisRange) - thisRange.startX)) return;
03126
03127 int realLine = thisRange.line;
03128 int startCol = thisRange.startCol;
03129
03130 KateTextCursor c(realLine, 0);
03131 m_view->renderer()->textWidth( c, startX() + m_textHintMouseX, startCol);
03132
03133 QString tmp;
03134
03135 emit m_view->needTextHint(c.line(), c.col(), tmp);
03136
03137 if (!tmp.isEmpty()) kdDebug(13030)<<"Hint text: "<<tmp<<endl;
03138 }
03139
03140 void KateViewInternal::focusInEvent (QFocusEvent *)
03141 {
03142 if (KApplication::cursorFlashTime() > 0)
03143 m_cursorTimer.start ( KApplication::cursorFlashTime() / 2 );
03144
03145 if (m_textHintEnabled)
03146 m_textHintTimer.start( m_textHintTimeout );
03147
03148 paintCursor();
03149
03150 m_doc->setActiveView( m_view );
03151
03152 emit m_view->gotFocus( m_view );
03153 }
03154
03155 void KateViewInternal::focusOutEvent (QFocusEvent *)
03156 {
03157 if( m_view->renderer() && ! m_view->m_codeCompletion->codeCompletionVisible() )
03158 {
03159 m_cursorTimer.stop();
03160
03161 m_view->renderer()->setDrawCaret(true);
03162 paintCursor();
03163 emit m_view->lostFocus( m_view );
03164 }
03165
03166 m_textHintTimer.stop();
03167 }
03168
03169 void KateViewInternal::doDrag()
03170 {
03171 dragInfo.state = diDragging;
03172 dragInfo.dragObject = new QTextDrag(m_view->selection(), this);
03173 dragInfo.dragObject->drag();
03174 }
03175
03176 void KateViewInternal::dragEnterEvent( QDragEnterEvent* event )
03177 {
03178 event->accept( (QTextDrag::canDecode(event) && m_doc->isReadWrite()) ||
03179 KURLDrag::canDecode(event) );
03180 }
03181
03182 void KateViewInternal::dragMoveEvent( QDragMoveEvent* event )
03183 {
03184
03185 placeCursor( event->pos(), true, false );
03186
03187
03188
03189 event->acceptAction();
03190 }
03191
03192 void KateViewInternal::dropEvent( QDropEvent* event )
03193 {
03194 if ( KURLDrag::canDecode(event) ) {
03195
03196 emit dropEventPass(event);
03197
03198 } else if ( QTextDrag::canDecode(event) && m_doc->isReadWrite() ) {
03199
03200 QString text;
03201
03202 if (!QTextDrag::decode(event, text))
03203 return;
03204
03205
03206 bool priv = false;
03207 if (event->source() && event->source()->inherits("KateViewInternal"))
03208 priv = m_doc->ownedView( ((KateViewInternal*)(event->source()))->m_view );
03209
03210
03211 bool selected = isTargetSelected( event->pos() );
03212
03213 if( priv && selected ) {
03214
03215
03216 return;
03217 }
03218
03219
03220 m_doc->editStart ();
03221
03222
03223 if ( event->action() != QDropEvent::Copy )
03224 m_view->removeSelectedText();
03225
03226 m_doc->insertText( cursor.line(), cursor.col(), text );
03227
03228 m_doc->editEnd ();
03229
03230 placeCursor( event->pos() );
03231
03232 event->acceptAction();
03233 updateView();
03234 }
03235
03236
03237 dragInfo.state = diNone;
03238
03239 stopDragScroll();
03240 }
03241
03242
03243 void KateViewInternal::clear()
03244 {
03245 cursor.setPos(0, 0);
03246 displayCursor.setPos(0, 0);
03247 }
03248
03249 void KateViewInternal::wheelEvent(QWheelEvent* e)
03250 {
03251 if (m_lineScroll->minValue() != m_lineScroll->maxValue() && e->orientation() != Qt::Horizontal) {
03252
03253 if ( ( e->state() & ControlButton ) || ( e->state() & ShiftButton ) ) {
03254 if (e->delta() > 0)
03255 scrollPrevPage();
03256 else
03257 scrollNextPage();
03258 } else {
03259 scrollViewLines(-((e->delta() / 120) * QApplication::wheelScrollLines()));
03260
03261 update();
03262 leftBorder->update();
03263 }
03264
03265 } else if (columnScrollingPossible()) {
03266 QWheelEvent copy = *e;
03267 QApplication::sendEvent(m_columnScroll, ©);
03268
03269 } else {
03270 e->ignore();
03271 }
03272 }
03273
03274 void KateViewInternal::startDragScroll()
03275 {
03276 if ( !m_dragScrollTimer.isActive() ) {
03277 m_dragScrollTimer.start( scrollTime );
03278 }
03279 }
03280
03281 void KateViewInternal::stopDragScroll()
03282 {
03283 m_dragScrollTimer.stop();
03284 updateView();
03285 }
03286
03287 void KateViewInternal::doDragScroll()
03288 {
03289 QPoint p = this->mapFromGlobal( QCursor::pos() );
03290
03291 int dx = 0, dy = 0;
03292 if ( p.y() < scrollMargin ) {
03293 dy = p.y() - scrollMargin;
03294 } else if ( p.y() > height() - scrollMargin ) {
03295 dy = scrollMargin - (height() - p.y());
03296 }
03297
03298 if ( p.x() < scrollMargin ) {
03299 dx = p.x() - scrollMargin;
03300 } else if ( p.x() > width() - scrollMargin ) {
03301 dx = scrollMargin - (width() - p.x());
03302 }
03303
03304 dy /= 4;
03305
03306 if (dy)
03307 scrollLines(startPos().line() + dy);
03308
03309 if (columnScrollingPossible () && dx)
03310 scrollColumns(kMin (m_startX + dx, m_columnScroll->maxValue()));
03311
03312 if (!dy && !dx)
03313 stopDragScroll();
03314 }
03315
03316 void KateViewInternal::enableTextHints(int timeout)
03317 {
03318 m_textHintTimeout=timeout;
03319 m_textHintEnabled=true;
03320 m_textHintTimer.start(timeout);
03321 }
03322
03323 void KateViewInternal::disableTextHints()
03324 {
03325 m_textHintEnabled=false;
03326 m_textHintTimer.stop ();
03327 }
03328
03329 bool KateViewInternal::columnScrollingPossible ()
03330 {
03331 return !m_view->dynWordWrap() && m_columnScroll->isEnabled() && (m_columnScroll->maxValue() > 0);
03332 }
03333
03334
03335 void KateViewInternal::editStart()
03336 {
03337 editSessionNumber++;
03338
03339 if (editSessionNumber > 1)
03340 return;
03341
03342 editIsRunning = true;
03343 editOldCursor = cursor;
03344 }
03345
03346 void KateViewInternal::editEnd(int editTagLineStart, int editTagLineEnd, bool tagFrom)
03347 {
03348 if (editSessionNumber == 0)
03349 return;
03350
03351 editSessionNumber--;
03352
03353 if (editSessionNumber > 0)
03354 return;
03355
03356 if (tagFrom && (editTagLineStart <= int(m_doc->getRealLine(startLine()))))
03357 tagAll();
03358 else
03359 tagLines (editTagLineStart, tagFrom ? m_doc->lastLine() : editTagLineEnd, true);
03360
03361 if (editOldCursor == cursor)
03362 updateBracketMarks();
03363
03364 if (m_imPreeditLength <= 0)
03365 updateView(true);
03366
03367 if ((editOldCursor != cursor) && (m_imPreeditLength <= 0))
03368 {
03369 m_madeVisible = false;
03370 updateCursor ( cursor, true );
03371 }
03372 else if ( m_view == m_doc->activeView() )
03373 {
03374 makeVisible(displayCursor, displayCursor.col());
03375 }
03376
03377 editIsRunning = false;
03378 }
03379
03380 void KateViewInternal::editSetCursor (const KateTextCursor &cursor)
03381 {
03382 if (this->cursor != cursor)
03383 {
03384 this->cursor.setPos (cursor);
03385 }
03386 }
03387
03388
03389 void KateViewInternal::viewSelectionChanged ()
03390 {
03391 if (!m_view->hasSelection())
03392 {
03393 selectAnchor.setPos (-1, -1);
03394 selStartCached.setPos (-1, -1);
03395 }
03396 }
03397
03398
03399 void KateViewInternal::imStartEvent( QIMEvent *e )
03400 {
03401 if ( m_doc->m_bReadOnly ) {
03402 e->ignore();
03403 return;
03404 }
03405
03406 if ( m_view->hasSelection() )
03407 m_view->removeSelectedText();
03408
03409 m_imPreeditStartLine = cursor.line();
03410 m_imPreeditStart = cursor.col();
03411 m_imPreeditLength = 0;
03412 m_imPreeditSelStart = m_imPreeditStart;
03413
03414 m_view->setIMSelectionValue( m_imPreeditStartLine, m_imPreeditStart, 0, 0, 0, true );
03415 }
03416
03417 void KateViewInternal::imComposeEvent( QIMEvent *e )
03418 {
03419 if ( m_doc->m_bReadOnly ) {
03420 e->ignore();
03421 return;
03422 }
03423
03424
03425 if ( m_imPreeditLength > 0 ) {
03426 cursor.setPos( m_imPreeditStartLine, m_imPreeditStart );
03427 m_doc->removeText( m_imPreeditStartLine, m_imPreeditStart,
03428 m_imPreeditStartLine, m_imPreeditStart + m_imPreeditLength );
03429 }
03430
03431 m_imPreeditLength = e->text().length();
03432 m_imPreeditSelStart = m_imPreeditStart + e->cursorPos();
03433
03434
03435 m_view->setIMSelectionValue( m_imPreeditStartLine, m_imPreeditStart, m_imPreeditStart + m_imPreeditLength,
03436 m_imPreeditSelStart, m_imPreeditSelStart + e->selectionLength(),
03437 true );
03438
03439
03440 m_doc->insertText( m_imPreeditStartLine, m_imPreeditStart, e->text() );
03441
03442
03443
03444 cursor.setPos( m_imPreeditStartLine, m_imPreeditSelStart );
03445 updateCursor( cursor, true );
03446
03447 updateView( true );
03448 }
03449
03450 void KateViewInternal::imEndEvent( QIMEvent *e )
03451 {
03452 if ( m_doc->m_bReadOnly ) {
03453 e->ignore();
03454 return;
03455 }
03456
03457 if ( m_imPreeditLength > 0 ) {
03458 cursor.setPos( m_imPreeditStartLine, m_imPreeditStart );
03459 m_doc->removeText( m_imPreeditStartLine, m_imPreeditStart,
03460 m_imPreeditStartLine, m_imPreeditStart + m_imPreeditLength );
03461 }
03462
03463 m_view->setIMSelectionValue( m_imPreeditStartLine, m_imPreeditStart, 0, 0, 0, false );
03464
03465 if ( e->text().length() > 0 ) {
03466 m_doc->insertText( cursor.line(), cursor.col(), e->text() );
03467
03468 if ( !m_cursorTimer.isActive() && KApplication::cursorFlashTime() > 0 )
03469 m_cursorTimer.start ( KApplication::cursorFlashTime() / 2 );
03470
03471 updateView( true );
03472 updateCursor( cursor, true );
03473 }
03474
03475 m_imPreeditStart = 0;
03476 m_imPreeditLength = 0;
03477 m_imPreeditSelStart = 0;
03478 }
03479
03480
03481