00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "khtml_caret_p.h"
00023
00024 #include "html/html_documentimpl.h"
00025
00026 namespace khtml {
00027
00035 enum ObjectAdvanceState {
00036 LeftObject = 0x01, AdvancedToSibling = 0x02, EnteredObject = 0x04
00037 };
00038
00047 enum ObjectTraversalState {
00048 OutsideDescending, InsideDescending, InsideAscending, OutsideAscending
00049 };
00050
00060 static RenderObject* traverseRenderObjects(RenderObject *obj,
00061 ObjectTraversalState &trav, bool toBegin, RenderObject *base,
00062 int &state)
00063 {
00064 RenderObject *r;
00065 switch (trav) {
00066 case OutsideDescending:
00067 trav = InsideDescending;
00068 break;
00069 case InsideDescending:
00070 r = toBegin ? obj->lastChild() : obj->firstChild();
00071 if (r) {
00072 trav = OutsideDescending;
00073 obj = r;
00074 state |= EnteredObject;
00075 } else {
00076 trav = InsideAscending;
00077 }
00078 break;
00079 case InsideAscending:
00080 trav = OutsideAscending;
00081 break;
00082 case OutsideAscending:
00083 r = toBegin ? obj->previousSibling() : obj->nextSibling();
00084 if (r) {
00085 trav = OutsideDescending;
00086 state |= AdvancedToSibling;
00087 } else {
00088 r = obj->parent();
00089 if (r == base) r = 0;
00090 trav = InsideAscending;
00091 state |= LeftObject;
00092 }
00093 obj = r;
00094 break;
00095 }
00096
00097 return obj;
00098 }
00099
00105 static inline RenderObject *renderObjectBelow(RenderObject *obj, ObjectTraversalState &trav, RenderObject *base)
00106 {
00107 trav = InsideDescending;
00108 int state;
00109 RenderObject *r = obj;
00110 while (r && trav != OutsideDescending) {
00111 r = traverseRenderObjects(r, trav, false, base, state);
00112 #if DEBUG_CARETMODE > 3
00113 kdDebug(6200) << "renderObjectBelow: r " << r << " trav " << trav << endl;
00114 #endif
00115 }
00116 trav = InsideDescending;
00117 return r;
00118 }
00119
00125 static inline RenderObject *renderObjectAbove(RenderObject *obj, ObjectTraversalState &trav, RenderObject *base)
00126 {
00127 trav = OutsideAscending;
00128 int state;
00129 RenderObject *r = obj;
00130 while (r && trav != InsideAscending) {
00131 r = traverseRenderObjects(r, trav, true, base, state);
00132 #if DEBUG_CARETMODE > 3
00133 kdDebug(6200) << "renderObjectAbove: r " << r << " trav " << trav << endl;
00134 #endif
00135 }
00136 trav = InsideAscending;
00137 return r;
00138 }
00139
00144 static inline bool isIndicatedInlineBox(InlineBox *box)
00145 {
00146
00147 if (box->isInlineTextBox()) return false;
00148 RenderStyle *s = box->object()->style();
00149 return s->borderLeftWidth() || s->borderRightWidth()
00150 || s->borderTopWidth() || s->borderBottomWidth()
00151 || s->paddingLeft().value() || s->paddingRight().value()
00152 || s->paddingTop().value() || s->paddingBottom().value()
00153
00154
00155 || s->marginLeft().value() || s->marginRight().value();
00156 }
00157
00162 static inline bool isIndicatedFlow(RenderObject *r)
00163 {
00164 RenderStyle *s = r->style();
00165 return s->borderLeftStyle() != BNONE || s->borderRightStyle() != BNONE
00166 || s->borderTopStyle() != BNONE || s->borderBottomStyle() != BNONE
00167
00168
00169
00170 || s->hasClip() || s->overflow() != OVISIBLE
00171 || s->backgroundColor().isValid() || s->backgroundImage();
00172 }
00173
00187 static RenderObject *advanceObject(RenderObject *r,
00188 ObjectTraversalState &trav, bool toBegin,
00189 RenderObject *base, int &state)
00190 {
00191
00192 ObjectTraversalState origtrav = trav;
00193 RenderObject *a = traverseRenderObjects(r, trav, toBegin, base, state);
00194
00195 bool ignoreOutsideDesc = toBegin && origtrav == OutsideAscending;
00196
00197
00198 RenderObject *la = 0;
00199 ObjectTraversalState latrav = trav;
00200 ObjectTraversalState lasttrav = origtrav;
00201
00202 while (a) {
00203 #if DEBUG_CARETMODE > 5
00204 kdDebug(6200) << "a " << a << " trav " << trav << endl;
00205 #endif
00206 if (a->element()) {
00207 #if DEBUG_CARETMODE > 4
00208 kdDebug(6200) << "a " << a << " trav " << trav << " origtrav " << origtrav << " ignoreOD " << ignoreOutsideDesc << endl;
00209 #endif
00210 if (toBegin) {
00211
00212 switch (origtrav) {
00213 case OutsideDescending:
00214 if (trav == InsideAscending) return a;
00215 if (trav == OutsideDescending) return a;
00216 break;
00217 case InsideDescending:
00218 if (trav == OutsideDescending) return a;
00219
00220 case InsideAscending:
00221 if (trav == OutsideAscending) return a;
00222 break;
00223 case OutsideAscending:
00224 if (trav == OutsideAscending) return a;
00225 if (trav == InsideAscending && lasttrav == InsideDescending) return a;
00226 if (trav == OutsideDescending && !ignoreOutsideDesc) return a;
00227
00228
00229
00230 la = a; latrav = trav;
00231 ignoreOutsideDesc = false;
00232 break;
00233 }
00234
00235 } else {
00236
00237 switch (origtrav) {
00238 case OutsideDescending:
00239 if (trav == InsideAscending) return a;
00240 if (trav == OutsideDescending) return a;
00241 break;
00242 case InsideDescending:
00243
00244
00245 case InsideAscending:
00246
00247
00248 case OutsideAscending:
00249
00250
00251
00252
00253 if (trav == OutsideDescending) return a;
00254 if (trav == OutsideAscending) {
00255 if (la) return la;
00256
00257
00258 la = a; latrav = trav;
00259 }
00260 break;
00261 }
00262
00263 }
00264 }
00265
00266 lasttrav = trav;
00267 a = traverseRenderObjects(a, trav, toBegin, base, state);
00268 }
00269
00270 if (la) trav = latrav, a = la;
00271 return a;
00272
00273 }
00274
00283 static inline bool isUnsuitable(RenderObject *r, ObjectTraversalState )
00284 {
00285 if (!r) return false;
00286 return r->isTableCol() || r->isTableSection() || r->isTableRow()
00287 || (r->isText() && static_cast<RenderText *>(r)->inlineTextBoxCount() == 0);
00288 ;
00289 }
00290
00304 static inline RenderObject *advanceSuitableObject(RenderObject *r,
00305 ObjectTraversalState &trav, bool toBegin,
00306 RenderObject *base, int &state)
00307 {
00308 do {
00309 r = advanceObject(r, trav, toBegin, base, state);
00310 #if DEBUG_CARETMODE > 2
00311 kdDebug(6200) << "after advanceSWP: r " << r << " trav " << trav << " toBegin " << toBegin << endl;
00312 #endif
00313 } while (isUnsuitable(r, trav));
00314 return r;
00315 }
00316
00326 static NodeImpl *nextLeafNode(NodeImpl *r, NodeImpl *baseElem)
00327 {
00328 NodeImpl *n = r->firstChild();
00329 if (n) {
00330 while (n) { r = n; n = n->firstChild(); }
00331 return const_cast<NodeImpl *>(r);
00332 }
00333 n = r->nextSibling();
00334 if (n) {
00335 r = n;
00336 while (n) { r = n; n = n->firstChild(); }
00337 return const_cast<NodeImpl *>(r);
00338 }
00339
00340 n = r->parentNode();
00341 if (n == baseElem) n = 0;
00342 while (n) {
00343 r = n;
00344 n = r->nextSibling();
00345 if (n) {
00346 r = n;
00347 n = r->firstChild();
00348 while (n) { r = n; n = n->firstChild(); }
00349 return const_cast<NodeImpl *>(r);
00350 }
00351 n = r->parentNode();
00352 if (n == baseElem) n = 0;
00353 }
00354 return 0;
00355 }
00356
00357 #if 0 // currently not used
00358
00367 static NodeImpl *prevLeafNode(NodeImpl *r, NodeImpl *baseElem)
00368 {
00369 NodeImpl *n = r->firstChild();
00370 if (n) {
00371 while (n) { r = n; n = n->firstChild(); }
00372 return const_cast<NodeImpl *>(r);
00373 }
00374 n = r->previousSibling();
00375 if (n) {
00376 r = n;
00377 while (n) { r = n; n = n->firstChild(); }
00378 return const_cast<NodeImpl *>(r);
00379 }
00380
00381 n = r->parentNode();
00382 if (n == baseElem) n = 0;
00383 while (n) {
00384 r = n;
00385 n = r->previousSibling();
00386 if (n) {
00387 r = n;
00388 n = r->lastChild();
00389 while (n) { r = n; n = n->lastChild(); }
00390 return const_cast<NodeImpl *>(r);
00391 }
00392 n = r->parentNode();
00393 if (n == baseElem) n = 0;
00394 }
00395 return 0;
00396 }
00397 #endif
00398
00410 void mapDOMPosToRenderPos(NodeImpl *node, long offset,
00411 RenderObject *&r, long &r_ofs, bool &outside, bool &outsideEnd)
00412 {
00413 if (node->nodeType() == Node::TEXT_NODE) {
00414 outside = false;
00415 outsideEnd = false;
00416 r = node->renderer();
00417 r_ofs = offset;
00418 } else if (node->nodeType() == Node::ELEMENT_NODE || node->nodeType() == Node::DOCUMENT_NODE) {
00419
00420
00421
00422 if (node->firstChild()) {
00423 outside = true;
00424 NodeImpl *child = offset <= 0 ? node->firstChild()
00425
00426 : node->childNode((unsigned long)offset);
00427
00428 bool atEnd = !child;
00429 #if DEBUG_CARETMODE > 5
00430 kdDebug(6200) << "mapDTR: child " << child << "@" << (child ? child->nodeName().string() : QString::null) << " atEnd " << atEnd << endl;
00431 #endif
00432 if (atEnd) child = node->lastChild();
00433
00434 r = child->renderer();
00435 r_ofs = 0;
00436 outsideEnd = atEnd;
00437
00438
00439
00440 if (r && child->nodeType() == Node::TEXT_NODE) {
00441 r = r->parent();
00442 RenderObject *o = node->renderer();
00443 while (o->continuation() && o->continuation() != r)
00444 o = o->continuation();
00445 if (!r || o->continuation() != r) {
00446 r = child->renderer();
00447 }
00448 }
00449
00450
00451
00452 if (r && r->isBR()) {
00453 r = r->objectAbove();
00454 outsideEnd = true;
00455 }
00456
00457 } else {
00458
00459 outside = false;
00460 outsideEnd = false;
00461 r = node->renderer();
00462 r_ofs = 0;
00463 }
00464
00465 } else {
00466 r = 0;
00467 kdWarning() << k_funcinfo << "Mapping from nodes of type " << node->nodeType()
00468 << " not supported!" << endl;
00469 }
00470 }
00471
00482 void mapRenderPosToDOMPos(RenderObject *r, long r_ofs,
00483 bool outside, bool outsideEnd, NodeImpl *&node, long &offset)
00484 {
00485 node = r->element();
00486 Q_ASSERT(node);
00487 #if DEBUG_CARETMODE > 5
00488 kdDebug(6200) << "mapRTD: r " << r << "@" << (r ? r->renderName() : QString::null) << (r && r->element() ? QString(".node ") + QString::number((unsigned)r->element(),16) + "@" + r->element()->nodeName().string() : QString::null) << " outside " << outside << " outsideEnd " << outsideEnd << endl;
00489 #endif
00490 if (node->nodeType() == Node::ELEMENT_NODE || node->nodeType() == Node::TEXT_NODE) {
00491
00492 if (outside) {
00493 NodeImpl *parent = node->parent();
00494
00495
00496
00497 if (r != node->renderer()) {
00498 RenderObject *o = node->renderer();
00499 while (o->continuation() && o->continuation() != r)
00500 o = o->continuation();
00501 if (o->continuation() == r) {
00502 parent = node;
00503
00504
00505 node = r->firstChild() ? r->firstChild()->element() : node;
00506 }
00507 }
00508
00509 if (!parent) goto inside;
00510
00511 offset = (long)node->nodeIndex() + outsideEnd;
00512 node = parent;
00513 #if DEBUG_CARETMODE > 5
00514 kdDebug(6200) << node << "@" << (node ? node->nodeName().string() : QString::null) << " offset " << offset << endl;
00515 #endif
00516 } else {
00517 inside:
00518 offset = r_ofs;
00519 }
00520
00521 } else {
00522 offset = 0;
00523 kdWarning() << k_funcinfo << "Mapping to nodes of type " << node->nodeType()
00524 << " not supported!" << endl;
00525 }
00526 }
00527
00529 static inline void ensureLeafNode(NodeImpl *&node, NodeImpl *base)
00530 {
00531 if (node && node->hasChildNodes()) node = nextLeafNode(node, base);
00532 }
00533
00540 static inline void mapRenderPosToTraversalState(bool outside, bool atEnd,
00541 bool toBegin, ObjectTraversalState &trav)
00542 {
00543 if (!outside) atEnd = !toBegin;
00544 if (!atEnd ^ toBegin)
00545 trav = outside ? OutsideDescending : InsideDescending;
00546 else
00547 trav = outside ? OutsideAscending : InsideAscending;
00548 }
00549
00556 static inline void mapTraversalStateToRenderPos(ObjectTraversalState trav,
00557 bool toBegin, bool &outside, bool &atEnd)
00558 {
00559 outside = false;
00560 switch (trav) {
00561 case OutsideDescending: outside = true;
00562 case InsideDescending: atEnd = toBegin; break;
00563 case OutsideAscending: outside = true;
00564 case InsideAscending: atEnd = !toBegin; break;
00565 }
00566 }
00567
00583 static RenderObject* findRenderer(NodeImpl *&node, long offset,
00584 RenderObject *base, long &r_ofs,
00585 bool &outside, bool &outsideEnd)
00586 {
00587 if (!node) return 0;
00588 RenderObject *r;
00589 mapDOMPosToRenderPos(node, offset, r, r_ofs, outside, outsideEnd);
00590 #if DEBUG_CARETMODE > 2
00591 kdDebug(6200) << "findRenderer: node " << node << " " << (node ? node->nodeName().string() : QString::null) << " offset " << offset << " r " << r << "[" << (r ? r->renderName() : QString::null) << "] r_ofs " << r_ofs << " outside " << outside << " outsideEnd " << outsideEnd << endl;
00592 #endif
00593 if (r) return r;
00594 NodeImpl *baseElem = base ? base->element() : 0;
00595 while (!r) {
00596 node = nextLeafNode(node, baseElem);
00597 if (!node) break;
00598 r = node->renderer();
00599 if (r) r_ofs = offset;
00600 }
00601 #if DEBUG_CARETMODE > 3
00602 kdDebug(6200) << "1r " << r << endl;
00603 #endif
00604 ObjectTraversalState trav;
00605 int state;
00606 mapRenderPosToTraversalState(outside, outsideEnd, false, trav);
00607 if (r && isUnsuitable(r, trav)) {
00608 r = advanceSuitableObject(r, trav, false, base, state);
00609 mapTraversalStateToRenderPos(trav, false, outside, outsideEnd);
00610 if (r) r_ofs = r->minOffset();
00611 }
00612 #if DEBUG_CARETMODE > 3
00613 kdDebug(6200) << "2r " << r << endl;
00614 #endif
00615 return r;
00616 }
00617
00621 static ElementImpl *determineBaseElement(NodeImpl *caretNode)
00622 {
00623
00624
00625
00626 DocumentImpl *doc = caretNode->getDocument();
00627 if (!doc) return 0;
00628
00629 if (doc->isHTMLDocument())
00630 return static_cast<HTMLDocumentImpl *>(doc)->body();
00631
00632 return 0;
00633 }
00634
00635
00636
00637 #if DEBUG_CARETMODE > 0
00638 void CaretBox::dump(QTextStream &ts, const QString &ind) const
00639 {
00640 ts << ind << "b@" << _box;
00641
00642 if (_box) {
00643 ts << "<" << _box->object() << ":" << _box->object()->renderName() << ">";
00644 }
00645
00646 ts << " " << _x << "+" << _y << "+" << _w << "*" << _h;
00647
00648 ts << " cb@" << cb;
00649 if (cb) ts << ":" << cb->renderName();
00650
00651 ts << " " << (_outside ? (outside_end ? "oe" : "o-") : "i-");
00652
00653 }
00654 #endif
00655
00656
00657
00658 #if DEBUG_CARETMODE > 0
00659 # define DEBUG_ACIB 1
00660 #else
00661 # define DEBUG_ACIB DEBUG_CARETMODE
00662 #endif
00663 void CaretBoxLine::addConvertedInlineBox(InlineBox *box, SeekBoxParams &sbp)
00664 {
00665
00666
00667 bool coalesceOutsideBoxes = false;
00668 CaretBoxIterator lastCoalescedBox;
00669 for (; box; box = box->nextOnLine()) {
00670 #if DEBUG_ACIB
00671 kdDebug(6200) << "box " << box << endl;
00672 kdDebug(6200) << "box->object " << box->object() << endl;
00673 kdDebug(6200) << "x " << box->m_x << " y " << box->m_y << " w " << box->m_width << " h " << box->m_height << " baseline " << box->m_baseline << " ifb " << box->isInlineFlowBox() << " itb " << box->isInlineTextBox() << " rlb " << box->isRootInlineBox() << endl;
00674 #endif
00675
00676 if (!box->object()) continue;
00677
00678 RenderStyle *s = box->object()->style(box->m_firstLine);
00679
00680 RenderStyle *ps = box->parent() && box->parent()->object()
00681 ? box->parent()->object()->style(box->parent()->m_firstLine)
00682 : s;
00683
00684 if (box->isInlineFlowBox()) {
00685 #if DEBUG_ACIB
00686 kdDebug(6200) << "isinlineflowbox " << box << endl;
00687 #endif
00688 InlineFlowBox *flowBox = static_cast<InlineFlowBox *>(box);
00689 bool rtl = ps->direction() == RTL;
00690 const QFontMetrics &pfm = ps->fontMetrics();
00691
00692 if (flowBox->includeLeftEdge()) {
00693
00694
00695
00696 if (coalesceOutsideBoxes) {
00697 if (sbp.equalsBox(flowBox, true, false)) {
00698 sbp.it = lastCoalescedBox;
00699 Q_ASSERT(!sbp.found);
00700 sbp.found = true;
00701 }
00702 } else {
00703 addCreatedFlowBoxEdge(flowBox, pfm, true, rtl);
00704 sbp.check(preEnd());
00705 }
00706 }
00707
00708 if (flowBox->firstChild()) {
00709 #if DEBUG_ACIB
00710 kdDebug(6200) << "this " << this << " flowBox " << flowBox << " firstChild " << flowBox->firstChild() << endl;
00711 kdDebug(6200) << "== recursive invocation" << endl;
00712 #endif
00713 addConvertedInlineBox(flowBox->firstChild(), sbp);
00714 #if DEBUG_ACIB
00715 kdDebug(6200) << "== recursive invocation end" << endl;
00716 #endif
00717 }
00718 else {
00719 addCreatedFlowBoxInside(flowBox, s->fontMetrics());
00720 sbp.check(preEnd());
00721 }
00722
00723 if (flowBox->includeRightEdge()) {
00724 addCreatedFlowBoxEdge(flowBox, pfm, false, rtl);
00725 lastCoalescedBox = preEnd();
00726 sbp.check(lastCoalescedBox);
00727 coalesceOutsideBoxes = true;
00728 }
00729
00730 } else if (box->isInlineTextBox()) {
00731 #if DEBUG_ACIB
00732 kdDebug(6200) << "isinlinetextbox " << box << (box->object() ? QString(" contains \"%1\"").arg(QConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), kMin(box->maxOffset() - box->minOffset(), 15L)).string()) : QString::null) << endl;
00733 #endif
00734 caret_boxes.append(new CaretBox(box, false, false));
00735 sbp.check(preEnd());
00736
00737 coalesceOutsideBoxes = false;
00738
00739 } else {
00740 #if DEBUG_ACIB
00741 kdDebug(6200) << "some replaced or what " << box << endl;
00742 #endif
00743
00744 bool rtl = ps->direction() == RTL;
00745 const QFontMetrics &pfm = ps->fontMetrics();
00746
00747 if (coalesceOutsideBoxes) {
00748 if (sbp.equalsBox(box, true, false)) {
00749 sbp.it = lastCoalescedBox;
00750 Q_ASSERT(!sbp.found);
00751 sbp.found = true;
00752 }
00753 } else {
00754 addCreatedInlineBoxEdge(box, pfm, true, rtl);
00755 sbp.check(preEnd());
00756 }
00757
00758 caret_boxes.append(new CaretBox(box, false, false));
00759 sbp.check(preEnd());
00760
00761 addCreatedInlineBoxEdge(box, pfm, false, rtl);
00762 lastCoalescedBox = preEnd();
00763 sbp.check(lastCoalescedBox);
00764 coalesceOutsideBoxes = true;
00765 }
00766 }
00767 }
00768 #undef DEBUG_ACIB
00769
00770 void CaretBoxLine::addCreatedFlowBoxInside(InlineFlowBox *flowBox, const QFontMetrics &fm)
00771 {
00772
00773 CaretBox *caretBox = new CaretBox(flowBox, false, false);
00774 caret_boxes.append(caretBox);
00775
00776
00777
00778
00779
00780 caretBox->_y += flowBox->baseline() - fm.ascent();
00781 caretBox->_h = fm.height();
00782 }
00783
00784 void CaretBoxLine::addCreatedFlowBoxEdge(InlineFlowBox *flowBox, const QFontMetrics &fm, bool left, bool rtl)
00785 {
00786 CaretBox *caretBox = new CaretBox(flowBox, true, !left);
00787 caret_boxes.append(caretBox);
00788
00789 if (left ^ rtl) caretBox->_x -= flowBox->paddingLeft() + flowBox->borderLeft() + 1;
00790 else caretBox->_x += caretBox->_w + flowBox->paddingRight() + flowBox->borderRight();
00791
00792 caretBox->_y += flowBox->baseline() - fm.ascent();
00793 caretBox->_h = fm.height();
00794 caretBox->_w = 1;
00795 }
00796
00797 void CaretBoxLine::addCreatedInlineBoxEdge(InlineBox *box, const QFontMetrics &fm, bool left, bool rtl)
00798 {
00799 CaretBox *caretBox = new CaretBox(box, true, !left);
00800 caret_boxes.append(caretBox);
00801
00802 if (left ^ rtl) caretBox->_x--;
00803 else caretBox->_x += caretBox->_w;
00804
00805 caretBox->_y += box->baseline() - fm.ascent();
00806 caretBox->_h = fm.height();
00807 caretBox->_w = 1;
00808 }
00809
00810 CaretBoxLine *CaretBoxLine::constructCaretBoxLine(CaretBoxLineDeleter *deleter,
00811 InlineFlowBox *basicFlowBox, InlineBox *seekBox, bool seekOutside,
00812 bool seekOutsideEnd, CaretBoxIterator &iter, RenderObject *seekObject)
00813
00814 {
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825 CaretBoxLine *result = new CaretBoxLine(basicFlowBox);
00826 deleter->append(result);
00827
00828 SeekBoxParams sbp(seekBox, seekOutside, seekOutsideEnd, seekObject, iter);
00829
00830
00831 result->addConvertedInlineBox(basicFlowBox, sbp);
00832
00833 if (!sbp.found) sbp.it = result->end();
00834
00835 return result;
00836 }
00837
00838 CaretBoxLine *CaretBoxLine::constructCaretBoxLine(CaretBoxLineDeleter *deleter,
00839 RenderBox *cb, bool outside, bool outsideEnd, CaretBoxIterator &iter)
00840 {
00841 int _x = cb->xPos();
00842 int _y = cb->yPos();
00843 int height;
00844 int width = 1;
00845
00846 if (outside) {
00847
00848 RenderStyle *s = cb->element() && cb->element()->parent()
00849 && cb->element()->parent()->renderer()
00850 ? cb->element()->parent()->renderer()->style()
00851 : cb->style();
00852 bool rtl = s->direction() == RTL;
00853
00854 const QFontMetrics &fm = s->fontMetrics();
00855 height = fm.height();
00856
00857 if (!outsideEnd) {
00858 _x--;
00859 } else {
00860 _x += cb->width();
00861 }
00862
00863 int hl = fm.leading() / 2;
00864 int baseline = cb->baselinePosition(false);
00865 if (!cb->isReplaced() || cb->style()->display() == BLOCK) {
00866 if (!outsideEnd ^ rtl)
00867 _y -= fm.leading() / 2;
00868 else
00869 _y += kMax(cb->height() - fm.ascent() - hl, 0);
00870 } else {
00871 _y += baseline - fm.ascent() - hl;
00872 }
00873
00874 } else {
00875
00876 RenderStyle *s = cb->style();
00877 const QFontMetrics &fm = s->fontMetrics();
00878 height = fm.height();
00879
00880 _x += cb->borderLeft() + cb->paddingLeft();
00881 _y += cb->borderTop() + cb->paddingTop();
00882
00883
00884 switch (s->textAlign()) {
00885 case LEFT:
00886 case TAAUTO:
00887 case JUSTIFY:
00888 break;
00889 case CENTER:
00890 case KONQ_CENTER:
00891 _x += cb->contentWidth() / 2;
00892 break;
00893 case RIGHT:
00894 _x += cb->contentWidth();
00895 break;
00896 }
00897 }
00898
00899 CaretBoxLine *result = new CaretBoxLine;
00900 deleter->append(result);
00901 result->caret_boxes.append(new CaretBox(_x, _y, width, height, cb,
00902 outside, outsideEnd));
00903 iter = result->begin();
00904 return result;
00905 }
00906
00907 #if DEBUG_CARETMODE > 0
00908 void CaretBoxLine::dump(QTextStream &ts, const QString &ind) const
00909 {
00910 ts << ind << "cbl: baseFlowBox@" << basefb << endl;
00911 QString ind2 = ind + " ";
00912 for (size_t i = 0; i < caret_boxes.size(); i++) {
00913 if (i > 0) ts << endl;
00914 caret_boxes[i]->dump(ts, ind2);
00915 }
00916 }
00917 #endif
00918
00919
00920
00928 inline InlineFlowBox *seekBaseFlowBox(InlineBox *b, RenderObject *base = 0)
00929 {
00930
00931 while (b->parent() && b->object() != base) {
00932 b = b->parent();
00933 }
00934 Q_ASSERT(b->isInlineFlowBox());
00935 return static_cast<InlineFlowBox *>(b);
00936 }
00937
00940 inline bool isBlockRenderReplaced(RenderObject *r)
00941 {
00942 return r->isRenderReplaced() && r->style()->display() == BLOCK;
00943 }
00944
00961 static CaretBoxLine* findCaretBoxLine(DOM::NodeImpl *node, long offset,
00962 CaretBoxLineDeleter *cblDeleter, RenderObject *base,
00963 long &r_ofs, CaretBoxIterator &caretBoxIt)
00964 {
00965 bool outside, outsideEnd;
00966 RenderObject *r = findRenderer(node, offset, base, r_ofs, outside, outsideEnd);
00967 if (!r) { return 0; }
00968 #if DEBUG_CARETMODE > 0
00969 kdDebug(6200) << "=================== findCaretBoxLine" << endl;
00970 kdDebug(6200) << "node " << node << " offset: " << offset << " r " << r->renderName() << "[" << r << "].node " << r->element()->nodeName().string() << "[" << r->element() << "]" << " r_ofs " << r_ofs << " outside " << outside << " outsideEnd " << outsideEnd << endl;
00971 #endif
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983 if (r->isText()) do {
00984 RenderText *t = static_cast<RenderText *>(r);
00985 int dummy;
00986 InlineBox *b = t->findInlineTextBox(offset, dummy, true);
00987
00988
00989
00990 if (!b) {
00991 if (t->m_lines.count() > 0)
00992 b = t->m_lines[t->m_lines.count() - 1];
00993 else
00994 break;
00995 }
00996 Q_ASSERT(b);
00997 outside = false;
00998 InlineFlowBox *baseFlowBox = seekBaseFlowBox(b, base);
00999 #if DEBUG_CARETMODE > 2
01000 kdDebug(6200) << "text-box b: " << b << " baseFlowBox: " << baseFlowBox << (b && b->object() ? QString(" contains \"%1\"").arg(QConstString(static_cast<RenderText *>(b->object())->str->s+b->minOffset(), kMin(b->maxOffset() - b->minOffset(), 15L)).string()) : QString::null) << endl;
01001 #endif
01002 #if 0
01003 if (t->containingBlock()->isListItem()) dumpLineBoxes(static_cast<RenderFlow *>(t->containingBlock()));
01004 #endif
01005 #if DEBUG_CARETMODE > 0
01006 kdDebug(6200) << "=================== end findCaretBoxLine (renderText)" << endl;
01007 #endif
01008 return CaretBoxLine::constructCaretBoxLine(cblDeleter, baseFlowBox,
01009 b, outside, outsideEnd, caretBoxIt);
01010 } while(false);
01011
01012
01013 bool isrepl = isBlockRenderReplaced(r);
01014 if (r->isRenderBlock() || r->isRenderInline() || isrepl) {
01015 RenderFlow *flow = static_cast<RenderFlow *>(r);
01016 InlineFlowBox *firstLineBox = isrepl ? 0 : flow->firstLineBox();
01017
01018
01019
01020
01021 if (isrepl || r->isRenderBlock() && (outside || !firstLineBox)
01022 || r->isRenderInline() && !firstLineBox) {
01023 #if DEBUG_CARETMODE > 0
01024 kdDebug(6200) << "=================== end findCaretBoxLine (box " << (outside ? (outsideEnd ? "outside end" : "outside begin") : "inside") << ")" << endl;
01025 #endif
01026 Q_ASSERT(r->isBox());
01027 return CaretBoxLine::constructCaretBoxLine(cblDeleter,
01028 static_cast<RenderBox *>(r), outside, outsideEnd, caretBoxIt);
01029 }
01030
01031 kdDebug(6200) << "firstlinebox " << firstLineBox << endl;
01032 InlineFlowBox *baseFlowBox = seekBaseFlowBox(firstLineBox, base);
01033 return CaretBoxLine::constructCaretBoxLine(cblDeleter, baseFlowBox,
01034 firstLineBox, outside, outsideEnd, caretBoxIt);
01035 }
01036
01037 RenderBlock *cb = r->containingBlock();
01038
01039 Q_ASSERT(cb);
01040
01041
01042
01043 if (!cb->isRenderBlock()) {
01044 kdWarning() << "containing block is no render block!!! crash imminent" << endl;
01045 }
01046
01047 InlineFlowBox *flowBox = cb->firstLineBox();
01048
01049
01050
01051 if (!flowBox) {
01052
01053
01054
01055
01056
01057 #if DEBUG_CARETMODE > 0
01058 kdDebug(6200) << "=================== end findCaretBoxLine (2)" << endl;
01059 #endif
01060 return CaretBoxLine::constructCaretBoxLine(cblDeleter, cb,
01061 outside, outsideEnd, caretBoxIt);
01062 }
01063
01064
01065
01066
01067 for (; flowBox; flowBox = static_cast<InlineFlowBox *>(flowBox->nextLineBox())) {
01068 #if DEBUG_CARETMODE > 0
01069 kdDebug(6200) << "[scan line]" << endl;
01070 #endif
01071
01072
01073 InlineFlowBox *baseFlowBox = seekBaseFlowBox(flowBox, base);
01074 CaretBoxLine *cbl = CaretBoxLine::constructCaretBoxLine(cblDeleter,
01075 baseFlowBox, 0, outside, outsideEnd, caretBoxIt, r);
01076 #if DEBUG_CARETMODE > 5
01077 kdDebug(6200) << cbl->information() << endl;
01078 #endif
01079 if (caretBoxIt != cbl->end()) {
01080 #if DEBUG_CARETMODE > 0
01081 kdDebug(6200) << "=================== end findCaretBoxLine (3)" << endl;
01082 #endif
01083 return cbl;
01084 }
01085 }
01086
01087
01088
01089
01090 Q_ASSERT(!flowBox);
01091 CaretBoxLine *cbl = findCaretBoxLine(nextLeafNode(node, base ? base->element() : 0), 0, cblDeleter, base, r_ofs, caretBoxIt);
01092 #if DEBUG_CARETMODE > 0
01093 kdDebug(6200) << "=================== end findCaretBoxLine" << endl;
01094 #endif
01095 return cbl;
01096 }
01097
01104 static inline RenderTable *findTableUpTo(RenderObject *r, RenderFlow *cb)
01105 {
01106 while (r && r != cb && !r->isTable()) r = r->parent();
01107 return r && r->isTable() ? static_cast<RenderTable *>(r) : 0;
01108 }
01109
01112 static inline bool isDescendant(RenderObject *r, RenderObject *cb)
01113 {
01114 while (r && r != cb) r = r->parent();
01115 return r;
01116 }
01117
01128 static bool containsEditableElement(KHTMLPart *part, RenderBlock *cb,
01129 RenderTable *&table, bool fromEnd = false)
01130 {
01131 RenderObject *r = cb;
01132 if (fromEnd)
01133 while (r->lastChild()) r = r->lastChild();
01134 else
01135 while (r->firstChild()) r = r->firstChild();
01136
01137 RenderTable *tempTable = 0;
01138 table = 0;
01139 bool withinCb;
01140
01141 ObjectTraversalState trav = InsideDescending;
01142 do {
01143 bool modWithinCb = withinCb = isDescendant(r, cb);
01144
01145
01146 if (!modWithinCb) {
01147 modWithinCb = true;
01148 r = cb;
01149 } else
01150 tempTable = findTableUpTo(r, cb);
01151
01152 #if DEBUG_CARETMODE > 1
01153 kdDebug(6201) << "cee: r " << (r ? r->renderName() : QString::null) << "@" << r << " cb " << cb << " withinCb " << withinCb << " modWithinCb " << modWithinCb << " tempTable " << tempTable << endl;
01154 #endif
01155 if (r && modWithinCb && r->element() && !isUnsuitable(r, trav)
01156 && (part->isCaretMode() || part->isEditable()
01157 || r->style()->userInput() == UI_ENABLED)) {
01158 table = tempTable;
01159 #if DEBUG_CARETMODE > 1
01160 kdDebug(6201) << "cee: editable" << endl;
01161 #endif
01162 return true;
01163 }
01164
01165
01166
01167
01168 r = fromEnd ? r->objectAbove() : r->objectBelow();
01169 } while (r && withinCb);
01170 return false;
01171 }
01172
01185 static bool containsEditableChildElement(KHTMLPart *part, RenderBlock *cb,
01186 RenderTable *&table, bool fromEnd, RenderObject *start)
01187 {
01188 int state = 0;
01189 ObjectTraversalState trav = OutsideAscending;
01190
01191 RenderObject *r = start;
01192 do {
01193 r = traverseRenderObjects(r, trav, fromEnd, cb->parent(), state);
01194 } while(r && !(state & AdvancedToSibling));
01195
01196
01197
01198
01199 if (!r) return false;
01200
01201 if (fromEnd)
01202 while (r->firstChild()) r = r->firstChild();
01203 else
01204 while (r->lastChild()) r = r->lastChild();
01205
01206 if (!r) return false;
01207
01208 RenderTable *tempTable = 0;
01209 table = 0;
01210 bool withinCb = false;
01211 do {
01212
01213 bool modWithinCb = withinCb = isDescendant(r, cb);
01214
01215
01216 if (!modWithinCb) {
01217 modWithinCb = true;
01218 r = cb;
01219 } else
01220 tempTable = findTableUpTo(r, cb);
01221
01222 #if DEBUG_CARETMODE > 1
01223 kdDebug(6201) << "cece: r " << (r ? r->renderName() : QString::null) << "@" << r << " cb " << cb << " withinCb " << withinCb << " modWithinCb " << modWithinCb << " tempTable " << tempTable << endl;
01224 #endif
01225 if (r && withinCb && r->element() && !isUnsuitable(r, trav)
01226 && (part->isCaretMode() || part->isEditable()
01227 || r->style()->userInput() == UI_ENABLED)) {
01228 table = tempTable;
01229 #if DEBUG_CARETMODE > 1
01230 kdDebug(6201) << "cece: editable" << endl;
01231 #endif
01232 return true;
01233 }
01234
01235 r = fromEnd ? r->objectAbove() : r->objectBelow();
01236 } while (withinCb);
01237 return false;
01238 }
01239
01240
01241
01242 LinearDocument::LinearDocument(KHTMLPart *part, NodeImpl *node, long offset,
01243 CaretAdvancePolicy advancePolicy, ElementImpl *baseElem)
01244 : node(node), offset(offset), m_part(part),
01245 advPol(advancePolicy), base(0)
01246 {
01247 if (node == 0) return;
01248
01249 if (baseElem) {
01250 RenderObject *b = baseElem->renderer();
01251 if (b && (b->isRenderBlock() || b->isRenderInline()))
01252 base = b;
01253 }
01254
01255 initPreBeginIterator();
01256 initEndIterator();
01257 }
01258
01259 LinearDocument::~LinearDocument()
01260 {
01261 }
01262
01263 int LinearDocument::count() const
01264 {
01265
01266 return 1;
01267 }
01268
01269 LinearDocument::Iterator LinearDocument::current()
01270 {
01271 return LineIterator(this, node, offset);
01272 }
01273
01274 LinearDocument::Iterator LinearDocument::begin()
01275 {
01276 NodeImpl *n = base ? base->element() : 0;
01277 if (!base) n = node ? node->getDocument() : 0;
01278 if (!n) return end();
01279
01280 n = n->firstChild();
01281 if (advPol == LeafsOnly)
01282 while (n->firstChild()) n = n->firstChild();
01283
01284 if (!n) return end();
01285 return LineIterator(this, n, n->minOffset());
01286 }
01287
01288 LinearDocument::Iterator LinearDocument::preEnd()
01289 {
01290 NodeImpl *n = base ? base->element() : 0;
01291 if (!base) n = node ? node->getDocument() : 0;
01292 if (!n) return preBegin();
01293
01294 n = n->lastChild();
01295 if (advPol == LeafsOnly)
01296 while (n->lastChild()) n = n->lastChild();
01297
01298 if (!n) return preBegin();
01299 return LineIterator(this, n, n->maxOffset());
01300 }
01301
01302 void LinearDocument::initPreBeginIterator()
01303 {
01304 _preBegin = LineIterator(this, 0, 0);
01305 }
01306
01307 void LinearDocument::initEndIterator()
01308 {
01309 _end = LineIterator(this, 0, 1);
01310 }
01311
01312
01313
01314 CaretBoxIterator LineIterator::currentBox ;
01315 long LineIterator::currentOffset ;
01316
01317 LineIterator::LineIterator(LinearDocument *l, DOM::NodeImpl *node, long offset)
01318 : lines(l)
01319 {
01320
01321 if (!node) { cbl = 0; return; }
01322 cbl = findCaretBoxLine(node, offset, &lines->cblDeleter,
01323 l->baseObject(), currentOffset, currentBox);
01324
01325 #if DEBUG_CARETMODE > 0
01326 if (!cbl) kdDebug(6200) << "no render object found!" << endl;
01327 #endif
01328 if (!cbl) return;
01329 #if DEBUG_CARETMODE > 1
01330 kdDebug(6200) << "LineIterator: offset " << offset << " outside " << cbl->isOutside() << endl;
01331 #endif
01332 #if DEBUG_CARETMODE > 3
01333 kdDebug(6200) << cbl->information() << endl;
01334 #endif
01335 if (currentBox == cbl->end()) {
01336 #if DEBUG_CARETMODE > 0
01337 kdDebug(6200) << "LineIterator: findCaretBoxLine failed" << endl;
01338 #endif
01339 cbl = 0;
01340 }
01341 }
01342
01343 void LineIterator::nextBlock()
01344 {
01345 RenderObject *base = lines->baseObject();
01346
01347 bool cb_outside = cbl->isOutside();
01348 bool cb_outside_end = cbl->isOutsideEnd();
01349
01350 {
01351 RenderObject *r = cbl->enclosingObject();
01352
01353 ObjectTraversalState trav;
01354 int state;
01355 mapRenderPosToTraversalState(cb_outside, cb_outside_end, false, trav);
01356 #if DEBUG_CARETMODE > 1
01357 kdDebug(6200) << "nextBlock: before adv r" << r << " " << (r ? r->renderName() : QString::null) << (r && r->isText() ? " contains \"" + QString(((RenderText *)r)->str->s, QMIN(((RenderText *)r)->str->l,15)) + "\"" : QString::null) << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
01358 #endif
01359 r = advanceSuitableObject(r, trav, false, base, state);
01360 if (!r) {
01361 cbl = 0;
01362 return;
01363 }
01364
01365 mapTraversalStateToRenderPos(trav, false, cb_outside, cb_outside_end);
01366 #if DEBUG_CARETMODE > 1
01367 kdDebug(6200) << "nextBlock: after r" << r << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
01368 #endif
01369 #if DEBUG_CARETMODE > 0
01370 kdDebug(6200) << "++: r " << r << "[" << (r?r->renderName():QString::null) << "]" << endl;
01371 #endif
01372
01373 RenderBlock *cb;
01374
01375
01376 bool isrepl = isBlockRenderReplaced(r);
01377 if (r->isRenderBlock() || isrepl) {
01378 RenderBox *cb = static_cast<RenderBox *>(r);
01379
01380 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
01381 cb_outside, cb_outside_end, currentBox);
01382
01383 #if DEBUG_CARETMODE > 0
01384 kdDebug(6200) << "r->isFlow is cb. continuation @" << cb->continuation() << endl;
01385 #endif
01386 return;
01387 } else {
01388 cb = r->containingBlock();
01389 Q_ASSERT(cb->isRenderBlock());
01390 }
01391 InlineFlowBox *flowBox = cb->firstLineBox();
01392 #if DEBUG_CARETMODE > 0
01393 kdDebug(6200) << "++: flowBox " << flowBox << " cb " << cb << "[" << (cb?cb->renderName()+QString(".node ")+QString::number((unsigned)cb->element(),16)+(cb->element()?"@"+cb->element()->nodeName().string():QString::null):QString::null) << "]" << endl;
01394 #endif
01395 Q_ASSERT(flowBox);
01396 if (!flowBox) {
01397 cb_outside = cb_outside_end = true;
01398 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
01399 cb_outside, cb_outside_end, currentBox);
01400 return;
01401 }
01402
01403 bool seekOutside = false, seekOutsideEnd = false;
01404 CaretBoxIterator it;
01405 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
01406 flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
01407 }
01408 }
01409
01410 void LineIterator::prevBlock()
01411 {
01412 RenderObject *base = lines->baseObject();
01413
01414 bool cb_outside = cbl->isOutside();
01415 bool cb_outside_end = cbl->isOutsideEnd();
01416
01417 {
01418 RenderObject *r = cbl->enclosingObject();
01419 if (r->isAnonymous() && !cb_outside)
01420 cb_outside = true, cb_outside_end = false;
01421
01422 ObjectTraversalState trav;
01423 int state;
01424 mapRenderPosToTraversalState(cb_outside, cb_outside_end, true, trav);
01425 #if DEBUG_CARETMODE > 1
01426 kdDebug(6200) << "prevBlock: before adv r" << r << " " << (r ? r->renderName() : QString::null) << (r && r->isText() ? " contains \"" + QString(((RenderText *)r)->str->s, QMIN(((RenderText *)r)->str->l,15)) + "\"" : QString::null) << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
01427 #endif
01428 r = advanceSuitableObject(r, trav, true, base, state);
01429 if (!r) {
01430 cbl = 0;
01431 return;
01432 }
01433
01434 mapTraversalStateToRenderPos(trav, true, cb_outside, cb_outside_end);
01435 #if DEBUG_CARETMODE > 1
01436 kdDebug(6200) << "prevBlock: after r" << r << " trav " << trav << " cb_outside " << cb_outside << " cb_outside_end " << cb_outside_end << endl;
01437 #endif
01438 #if DEBUG_CARETMODE > 0
01439 kdDebug(6200) << "--: r " << r << "[" << (r?r->renderName():QString::null) << "]" << endl;
01440 #endif
01441
01442 RenderBlock *cb;
01443
01444
01445 bool isrepl = isBlockRenderReplaced(r);
01446
01447 if (r->isRenderBlock() || isrepl) {
01448 RenderBox *cb = static_cast<RenderBox *>(r);
01449
01450 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
01451 cb_outside, cb_outside_end, currentBox);
01452
01453 #if DEBUG_CARETMODE > 0
01454 kdDebug(6200) << "r->isFlow is cb. continuation @" << cb->continuation() << endl;
01455 #endif
01456 return;
01457 } else {
01458 cb = r->containingBlock();
01459 Q_ASSERT(cb->isRenderBlock());
01460 }
01461 InlineFlowBox *flowBox = cb->lastLineBox();
01462 #if DEBUG_CARETMODE > 0
01463 kdDebug(6200) << "--: flowBox " << flowBox << " cb " << cb << "[" << (cb?cb->renderName()+QString(".node ")+QString::number((unsigned)cb->element(),16)+(cb->element()?"@"+cb->element()->nodeName().string():QString::null):QString::null) << "]" << endl;
01464 #endif
01465 Q_ASSERT(flowBox);
01466 if (!flowBox) {
01467 cb_outside = true; cb_outside_end = false;
01468 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter, cb,
01469 cb_outside, cb_outside_end, currentBox);
01470 return;
01471 }
01472
01473 bool seekOutside = false, seekOutsideEnd = false;
01474 CaretBoxIterator it;
01475 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
01476 flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
01477 }
01478 }
01479
01480 void LineIterator::advance(bool toBegin)
01481 {
01482 InlineFlowBox *flowBox = cbl->baseFlowBox();
01483 if (flowBox) {
01484 flowBox = static_cast<InlineFlowBox *>(toBegin ? flowBox->prevLineBox() : flowBox->nextLineBox());
01485 if (flowBox) {
01486 bool seekOutside = false, seekOutsideEnd = false;
01487 CaretBoxIterator it;
01488 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
01489 flowBox, flowBox->firstChild(), seekOutside, seekOutsideEnd, it);
01490 }
01491 }
01492
01493
01494 if (!flowBox) { if (toBegin) prevBlock(); else nextBlock(); }
01495
01496 #if DEBUG_CARETMODE > 3
01497 if (cbl) kdDebug(6200) << cbl->information() << endl;
01498 #endif
01499 }
01500
01501
01502
01503 void EditableCaretBoxIterator::advance(bool toBegin)
01504 {
01505 #if DEBUG_CARETMODE > 3
01506 kdDebug(6200) << "---------------" << k_funcinfo << "toBegin " << toBegin << endl;
01507 #endif
01508 const CaretBoxIterator preBegin = cbl->preBegin();
01509 const CaretBoxIterator end = cbl->end();
01510
01511 CaretBoxIterator lastbox = *this, curbox;
01512 bool islastuseable = true;
01513 bool iscuruseable;
01514
01515 adjacent = true;
01516
01517 #if DEBUG_CARETMODE > 4
01518
01519 #endif
01520
01521 if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
01522 bool curAtEnd = *this == preBegin || *this == end;
01523 curbox = *this;
01524 bool atEnd = true;
01525 if (!curAtEnd) {
01526 iscuruseable = isEditable(curbox, toBegin);
01527 if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
01528 atEnd = *this == preBegin || *this == end;
01529 }
01530 while (!curAtEnd) {
01531 bool haslast = lastbox != end && lastbox != preBegin;
01532 bool hascoming = !atEnd;
01533 bool iscominguseable = true;
01534
01535 if (!atEnd) iscominguseable = isEditable(*this, toBegin);
01536 if (iscuruseable) {
01537 #if DEBUG_CARETMODE > 3
01538 kdDebug(6200) << "ebit::advance: " << (*curbox)->object() << "@" << (*curbox)->object()->renderName() << ".node " << (*curbox)->object()->element() << "[" << ((*curbox)->object()->element() ? (*curbox)->object()->element()->nodeName().string() : QString::null) << "] inline " << (*curbox)->isInline() << " outside " << (*curbox)->isOutside() << " outsideEnd " << (*curbox)->isOutsideEnd() << endl;
01539 #endif
01540
01541 CaretBox *box = *curbox;
01542 if (box->isOutside()) {
01543
01544
01545 if (!box->isInline()) break;
01546
01547 if (advpol == VisibleFlows) break;
01548
01549
01550
01551 InlineBox *ibox = box->inlineBox();
01552
01553 InlineBox *prev = box->isOutsideEnd() ? ibox : ibox->prevOnLine();
01554
01555 InlineBox *next = box->isOutsideEnd() ? ibox->nextOnLine() : ibox;
01556
01557 const bool isprevindicated = !prev || isIndicatedInlineBox(prev);
01558 const bool isnextindicated = !next || isIndicatedInlineBox(next);
01559 const bool last = haslast && !islastuseable;
01560 const bool coming = hascoming && !iscominguseable;
01561 const bool left = !prev || prev->isInlineFlowBox() && isprevindicated
01562 || (toBegin && coming || !toBegin && last);
01563 const bool right = !next || next->isInlineFlowBox() && isnextindicated
01564 || (!toBegin && coming || toBegin && last);
01565 const bool text2indicated = toBegin && next && next->isInlineTextBox()
01566 && isprevindicated
01567 || !toBegin && prev && prev->isInlineTextBox() && isnextindicated;
01568 const bool indicated2text = !toBegin && next && next->isInlineTextBox()
01569 && prev && isprevindicated
01570
01571 ;
01572 #if DEBUG_CARETMODE > 5
01573 kdDebug(6200) << "prev " << prev << " haslast " << haslast << " islastuseable " << islastuseable << " left " << left << " next " << next << " hascoming " << hascoming << " iscominguseable " << iscominguseable << " right " << right << " text2indicated " << text2indicated << " indicated2text " << indicated2text << endl;
01574 #endif
01575
01576 if (left && right && !text2indicated || indicated2text) {
01577 adjacent = false;
01578 #if DEBUG_CARETMODE > 4
01579 kdDebug(6200) << "left && right && !text2indicated || indicated2text" << endl;
01580 #endif
01581 break;
01582 }
01583
01584 } else {
01585
01586 #if DEBUG_CARETMODE > 4
01587 if (box->isInline()) {
01588 InlineBox *ibox = box->inlineBox();
01589 kdDebug(6200) << "inside " << (!ibox->isInlineFlowBox() || static_cast<InlineFlowBox *>(ibox)->firstChild() ? "non-empty" : "empty") << (isIndicatedInlineBox(ibox) ? " indicated" : "") << " adjacent=" << adjacent << endl;
01590 }
01591 #if 0
01592 RenderStyle *s = ibox->object()->style();
01593 kdDebug(6200) << "bordls " << s->borderLeftStyle()
01594 << " bordl " << (s->borderLeftStyle() != BNONE)
01595 << " bordr " << (s->borderRightStyle() != BNONE)
01596 << " bordt " << (s->borderTopStyle() != BNONE)
01597 << " bordb " << (s->borderBottomStyle() != BNONE)
01598 << " padl " << s->paddingLeft().value()
01599 << " padr " << s->paddingRight().value()
01600 << " padt " << s->paddingTop().value()
01601 << " padb " << s->paddingBottom().value()
01602
01603
01604 << " marl " << s->marginLeft().value()
01605 << " marr " << s->marginRight().value()
01606 << endl;
01607 #endif
01608 #endif
01609 break;
01610 }
01611
01612 } else {
01613
01614 if (!(*curbox)->isOutside()) {
01615
01616 adjacent = false;
01617 }
01618
01619 }
01620 lastbox = curbox;
01621 islastuseable = iscuruseable;
01622 curbox = *this;
01623 iscuruseable = iscominguseable;
01624 curAtEnd = atEnd;
01625 if (!atEnd) {
01626 if (toBegin) CaretBoxIterator::operator --(); else CaretBoxIterator::operator ++();
01627 atEnd = *this == preBegin || *this == end;
01628 }
01629 }
01630
01631 *static_cast<CaretBoxIterator *>(this) = curbox;
01632 #if DEBUG_CARETMODE > 4
01633
01634 #endif
01635 #if DEBUG_CARETMODE > 3
01636 kdDebug(6200) << "---------------" << k_funcinfo << "end " << endl;
01637 #endif
01638 }
01639
01640 bool EditableCaretBoxIterator::isEditable(const CaretBoxIterator &boxit, bool fromEnd)
01641 {
01642 Q_ASSERT(boxit != cbl->end() && boxit != cbl->preBegin());
01643 CaretBox *b = *boxit;
01644 RenderObject *r = b->object();
01645 #if DEBUG_CARETMODE > 0
01646
01647 kdDebug(6200) << "isEditable r" << r << ": " << (r ? r->renderName() : QString::null) << (r && r->isText() ? " contains \"" + QString(((RenderText *)r)->str->s, QMIN(((RenderText *)r)->str->l,15)) + "\"" : QString::null) << endl;
01648 #endif
01649
01650
01651
01652 NodeImpl *node = r->element();
01653 ObjectTraversalState trav;
01654 mapRenderPosToTraversalState(b->isOutside(), b->isOutsideEnd(), fromEnd, trav);
01655 if (isUnsuitable(r, trav) || !node) {
01656 return false;
01657 }
01658
01659
01660 if (!b->isOutside() && r->isRenderReplaced() && !r->firstChild())
01661 return false;
01662
01663 RenderObject *eff_r = r;
01664 bool globallyNavigable = m_part->isCaretMode() || m_part->isEditable();
01665
01666
01667 if (b->isOutside() && !globallyNavigable) {
01668 NodeImpl *par = node->parent();
01669
01670
01671 Q_ASSERT(par);
01672 if (par) node = par;
01673 eff_r = node->renderer();
01674 Q_ASSERT(eff_r);
01675 }
01676
01677 bool result = globallyNavigable || eff_r->style()->userInput() == UI_ENABLED;
01678 #if DEBUG_CARETMODE > 0
01679 kdDebug(6200) << result << endl;
01680 #endif
01681 return result;
01682 }
01683
01684
01685
01686 void EditableLineIterator::advance(bool toBegin)
01687 {
01688 CaretAdvancePolicy advpol = lines->advancePolicy();
01689 LineIterator lasteditable, lastindicated;
01690 bool haslasteditable = false;
01691 bool haslastindicated = false;
01692 bool uselasteditable = false;
01693
01694 LineIterator::advance(toBegin);
01695 while (cbl) {
01696 if (isEditable(*this)) {
01697 #if DEBUG_CARETMODE > 3
01698 kdDebug(6200) << "advance: " << cbl->enclosingObject() << "@" << cbl->enclosingObject()->renderName() << ".node " << cbl->enclosingObject()->element() << "[" << (cbl->enclosingObject()->element() ? cbl->enclosingObject()->element()->nodeName().string() : QString::null) << "]" << endl;
01699 #endif
01700
01701 bool hasindicated = isIndicatedFlow(cbl->enclosingObject());
01702 if (hasindicated) {
01703 haslastindicated = true;
01704 lastindicated = *this;
01705 }
01706
01707 switch (advpol) {
01708 case IndicatedFlows:
01709 if (hasindicated) goto wend;
01710
01711 case LeafsOnly:
01712 if (cbl->isOutside()) break;
01713
01714 case VisibleFlows: goto wend;
01715 }
01716
01717
01718 lasteditable = *this;
01719 haslasteditable = true;
01720 #if DEBUG_CARETMODE > 4
01721 kdDebug(6200) << "remembered lasteditable " << *lasteditable << endl;
01722 #endif
01723 } else {
01724
01725
01726
01727
01728
01729 if (haslasteditable) { uselasteditable = true; break; }
01730
01731 }
01732 LineIterator::advance(toBegin);
01733 }
01734 wend:
01735
01736 if (uselasteditable) *this = haslastindicated ? lastindicated : lasteditable;
01737 if (!cbl && haslastindicated) *this = lastindicated;
01738 }
01739
01740
01741
01742 void EditableCharacterIterator::initFirstChar()
01743 {
01744 CaretBox *box = *ebit;
01745 InlineBox *b = box->inlineBox();
01746 if (_offset == box->maxOffset())
01747 peekNext();
01748 else if (b && !box->isOutside() && b->isInlineTextBox())
01749 _char = static_cast<RenderText *>(b->object())->str->s[_offset].unicode();
01750 else
01751 _char = -1;
01752 }
01753
01757 static inline bool isCaretBoxEmpty(CaretBox *box) {
01758 if (!box->isInline()) return false;
01759 InlineBox *ibox = box->inlineBox();
01760 return ibox->isInlineFlowBox()
01761 && !static_cast<InlineFlowBox *>(ibox)->firstChild()
01762 && !isIndicatedInlineBox(ibox);
01763 }
01764
01765 EditableCharacterIterator &EditableCharacterIterator::operator ++()
01766 {
01767 _offset++;
01768
01769 CaretBox *box = *ebit;
01770 InlineBox *b = box->inlineBox();
01771 long maxofs = box->maxOffset();
01772 #if DEBUG_CARETMODE > 0
01773 kdDebug(6200) << "box->maxOffset() " << box->maxOffset() << " box->minOffset() " << box->minOffset() << endl;
01774 #endif
01775 if (_offset == maxofs) {
01776 #if DEBUG_CARETMODE > 2
01777 kdDebug(6200) << "_offset == maxofs: " << _offset << " == " << maxofs << endl;
01778 #endif
01779 peekNext();
01780 } else if (_offset > maxofs) {
01781 #if DEBUG_CARETMODE > 2
01782 kdDebug(6200) << "_offset > maxofs: " << _offset << " > " << maxofs << endl;
01783 #endif
01784 if (true) {
01785 ++ebit;
01786 if (ebit == (*_it)->end()) {
01787 ++_it;
01788 #if DEBUG_CARETMODE > 3
01789 kdDebug(6200) << "++_it" << endl;
01790 #endif
01791 if (_it != _it.lines->end()) {
01792 ebit = _it;
01793 box = *ebit;
01794 b = box->inlineBox();
01795 #if DEBUG_CARETMODE > 3
01796 kdDebug(6200) << "box " << box << " b " << b << " isText " << box->isInlineTextBox() << endl;
01797 #endif
01798
01799 #if DEBUG_CARETMODE > 3
01800 RenderObject *_r = box->object();
01801 kdDebug(6200) << "_r " << _r << ":" << _r->element()->nodeName().string() << endl;
01802 #endif
01803 _offset = box->minOffset();
01804 #if DEBUG_CARETMODE > 3
01805 kdDebug(6200) << "_offset " << _offset << endl;
01806 #endif
01807 } else {
01808 b = 0;
01809 _end = true;
01810 }
01811 goto readchar;
01812 }
01813 }
01814
01815 bool adjacent = ebit.isAdjacent();
01816 #if 0
01817
01818 if (adjacent && !(*ebit)->isInlineTextBox()) {
01819 EditableCaretBoxIterator copy = ebit;
01820 ++ebit;
01821 if (ebit != (*_it)->end() && (*ebit)->isInlineTextBox()
01822
01823 )
01824 adjacent = false;
01825 else ebit = copy;
01826 }
01827 #endif
01828
01829 if (adjacent && !(*ebit)->isInlineTextBox()) {
01830 bool noemptybox = true;
01831 while (isCaretBoxEmpty(*ebit)) {
01832 noemptybox = false;
01833 EditableCaretBoxIterator copy = ebit;
01834 ++ebit;
01835 if (ebit == (*_it)->end()) { ebit = copy; break; }
01836 }
01837 if (noemptybox) adjacent = false;
01838 }
01839
01840 _offset = (*ebit)->minOffset() + adjacent;
01841
01842 box = *ebit;
01843 b = box->inlineBox();
01844 goto readchar;
01845 } else {
01846 readchar:
01847
01848 if (b && !box->isOutside() && b->isInlineTextBox() && _offset < b->maxOffset())
01849 _char = static_cast<RenderText *>(b->object())->str->s[_offset].unicode();
01850 else
01851 _char = -1;
01852 }
01853 #if DEBUG_CARETMODE > 2
01854 kdDebug(6200) << "_offset: " << _offset << " char '" << (char)_char << "'" << endl;
01855 #endif
01856
01857 #if DEBUG_CARETMODE > 0
01858 if (!_end && ebit != (*_it)->end()) {
01859 CaretBox *box = *ebit;
01860 RenderObject *_r = box->object();
01861 kdDebug(6200) << "echit++(1): box " << box << (box && box->isInlineTextBox() ? QString(" contains \"%1\"").arg(QConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : QString::null) << " _r " << (_r ? _r->element()->nodeName().string() : QString("<nil>")) << endl;
01862 }
01863 #endif
01864 return *this;
01865 }
01866
01867 EditableCharacterIterator &EditableCharacterIterator::operator --()
01868 {
01869 _offset--;
01870
01871
01872 CaretBox *box = *ebit;
01873 CaretBox *_peekPrev = 0;
01874 CaretBox *_peekNext = 0;
01875 InlineBox *b = box->inlineBox();
01876 long minofs = box->minOffset();
01877 #if DEBUG_CARETMODE > 0
01878 kdDebug(6200) << "box->maxOffset() " << box->maxOffset() << " box->minOffset() " << box->minOffset() << endl;
01879 #endif
01880 if (_offset == minofs) {
01881 #if DEBUG_CARETMODE > 2
01882 kdDebug(6200) << "_offset == minofs: " << _offset << " == " << minofs << endl;
01883 #endif
01884
01885
01886 if (b && !box->isOutside() && b->isInlineTextBox())
01887 _char = static_cast<RenderText *>(b->object())->text()[_offset].unicode();
01888 else
01889 _char = -1;
01890
01891
01892 bool do_prev = false;
01893 {
01894 EditableCaretBoxIterator copy;
01895 _peekPrev = 0;
01896 do {
01897 copy = ebit;
01898 --ebit;
01899 if (ebit == (*_it)->preBegin()) { ebit = copy; break; }
01900 } while (isCaretBoxEmpty(*ebit));
01901
01902 if (ebit.isAdjacent() && ebit != (*_it)->preBegin() && (*ebit)->isInlineTextBox()) {
01903 _peekPrev = *ebit;
01904 do_prev = true;
01905 } else
01906 ebit = copy;
01907 }
01908 if (do_prev) goto prev;
01909 } else if (_offset < minofs) {
01910 prev:
01911 #if DEBUG_CARETMODE > 2
01912 kdDebug(6200) << "_offset < minofs: " << _offset << " < " << minofs << endl;
01913 #endif
01914 if (!_peekPrev) {
01915 _peekNext = *ebit;
01916 --ebit;
01917 if (ebit == (*_it)->preBegin()) {
01918 --_it;
01919 #if DEBUG_CARETMODE > 3
01920 kdDebug(6200) << "--_it" << endl;
01921 #endif
01922 if (_it != _it.lines->preBegin()) {
01923
01924 ebit = EditableCaretBoxIterator(_it, true);
01925 box = *ebit;
01926
01927 #if DEBUG_CARETMODE > 3
01928 kdDebug(6200) << "box " << box << " b " << box->inlineBox() << " isText " << box->isInlineTextBox() << endl;
01929 #endif
01930 _offset = box->maxOffset();
01931
01932 _char = -1;
01933 #if DEBUG_CARETMODE > 0
01934 kdDebug(6200) << "echit--(2): box " << box << " b " << box->inlineBox() << (box->isInlineTextBox() ? QString(" contains \"%1\"").arg(QConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : QString::null) << endl;
01935 #endif
01936 } else
01937 _end = true;
01938 return *this;
01939 }
01940 }
01941
01942 #if 0
01943 bool adjacent = ebit.isAdjacent();
01944 #endif
01945
01946 #if DEBUG_CARETMODE > 0
01947 kdDebug(6200) << "adjacent " << adjacent << " _peekNext " << _peekNext << " _peekNext->isInlineTextBox: " << (_peekNext ? _peekNext->isInlineTextBox() : false) << " !((*ebit)->isInlineTextBox): " << (*ebit ? !(*ebit)->isInlineTextBox() : true) << endl;
01948 #endif
01949 #if 0
01950
01951 if (adjacent && _peekNext && _peekNext->isInlineTextBox()
01952 && !(*ebit)->isInlineTextBox()) {
01953 EditableCaretBoxIterator copy = ebit;
01954 --ebit;
01955 if (ebit == (*_it)->preBegin())
01956 ebit = copy;
01957 }
01958 #endif
01959 #if 0
01960
01961 if (adjacent
01962 && !(*ebit)->isInlineTextBox()) {
01963 bool noemptybox = true;
01964 while (isCaretBoxEmpty(*ebit)) {
01965 noemptybox = false;
01966 EditableCaretBoxIterator copy = ebit;
01967 --ebit;
01968 if (ebit == (*_it)->preBegin()) { ebit = copy; break; }
01969 else _peekNext = *copy;
01970 }
01971 if (noemptybox) adjacent = false;
01972 }
01973 #endif
01974 #if DEBUG_CARETMODE > 0
01975 kdDebug(6200) << "(*ebit)->obj " << (*ebit)->object()->renderName() << "[" << (*ebit)->object() << "]" << " minOffset: " << (*ebit)->minOffset() << " maxOffset: " << (*ebit)->maxOffset() << endl;
01976 #endif
01977 #if DEBUG_CARETMODE > 3
01978 RenderObject *_r = (*ebit)->object();
01979 kdDebug(6200) << "_r " << _r << ":" << _r->element()->nodeName().string() << endl;
01980 #endif
01981 _offset = (*ebit)->maxOffset();
01982
01983 #if DEBUG_CARETMODE > 3
01984 kdDebug(6200) << "_offset " << _offset << endl;
01985 #endif
01986 _peekPrev = 0;
01987 } else {
01988 #if DEBUG_CARETMODE > 0
01989 kdDebug(6200) << "_offset: " << _offset << " _peekNext: " << _peekNext << endl;
01990 #endif
01991
01992 if (_peekNext && _offset >= box->maxOffset() && _peekNext->isInlineTextBox())
01993 _char = static_cast<RenderText *>(_peekNext->object())->text()[_peekNext->minOffset()].unicode();
01994 else if (b && _offset < b->maxOffset() && b->isInlineTextBox())
01995 _char = static_cast<RenderText *>(b->object())->text()[_offset].unicode();
01996 else
01997 _char = -1;
01998 }
01999
02000 #if DEBUG_CARETMODE > 0
02001 if (!_end && ebit != (*_it)->preBegin()) {
02002 CaretBox *box = *ebit;
02003 kdDebug(6200) << "echit--(1): box " << box << " b " << box->inlineBox() << (box->isInlineTextBox() ? QString(" contains \"%1\"").arg(QConstString(static_cast<RenderText *>(box->object())->str->s+box->minOffset(), box->maxOffset() - box->minOffset()).string()) : QString::null) << endl;
02004 }
02005 #endif
02006 return *this;
02007 }
02008
02009
02010
02011 TableRowIterator::TableRowIterator(RenderTable *table, bool fromEnd,
02012 RenderTableSection::RowStruct *row)
02013 : sec(table, fromEnd)
02014 {
02015
02016 if (*sec) {
02017 if (fromEnd) index = (*sec)->grid.size() - 1;
02018 else index = 0;
02019 }
02020
02021
02022 if (row && *sec) {
02023 while (operator *() != row)
02024 if (fromEnd) operator --(); else operator ++();
02025 }
02026 }
02027
02028 TableRowIterator &TableRowIterator::operator ++()
02029 {
02030 index++;
02031
02032 if (index >= (int)(*sec)->grid.size()) {
02033 ++sec;
02034
02035 if (*sec) index = 0;
02036 }
02037 return *this;
02038 }
02039
02040 TableRowIterator &TableRowIterator::operator --()
02041 {
02042 index--;
02043
02044 if (index < 0) {
02045 --sec;
02046
02047 if (*sec) index = (*sec)->grid.size() - 1;
02048 }
02049 return *this;
02050 }
02051
02052
02053
02054
02055 static RenderTableCell *findNearestTableCellInRow(KHTMLPart *part, int x,
02056 RenderTableSection::RowStruct *row, bool fromEnd);
02057
02071 static inline RenderTableCell *findNearestTableCell(KHTMLPart *part, int x,
02072 TableRowIterator &it, bool fromEnd)
02073 {
02074 RenderTableCell *result = 0;
02075
02076 while (*it) {
02077 result = findNearestTableCellInRow(part, x, *it, fromEnd);
02078 if (result) break;
02079
02080 if (fromEnd) --it; else ++it;
02081 }
02082
02083 return result;
02084 }
02085
02099 static RenderTableCell *findNearestTableCellInRow(KHTMLPart *part, int x,
02100 RenderTableSection::RowStruct *row, bool fromEnd)
02101 {
02102
02103 int n = (int)row->row->size();
02104 int i;
02105 for (i = 0; i < n; i++) {
02106 RenderTableCell *cell = row->row->at(i);
02107 if (!cell || (int)cell == -1) continue;
02108
02109 int absx, absy;
02110 cell->absolutePosition(absx, absy, false);
02111 #if DEBUG_CARETMODE > 1
02112 kdDebug(6201) << "i/n " << i << "/" << n << " absx " << absx << " absy " << absy << endl;
02113 #endif
02114
02115
02116
02117 #if DEBUG_CARETMODE > 1
02118 kdDebug(6201) << "x " << x << " < " << (absx + cell->width()) << "?" << endl;
02119 #endif
02120 if (x < absx + cell->width()) break;
02121 }
02122 if (i >= n) i = n - 1;
02123
02124
02125
02126 for (int cnt = 0; cnt < 2*n; cnt++) {
02127 int index = i - ((cnt >> 1) + 1)*(cnt & 1) + (cnt >> 1)*!(cnt & 1);
02128 if (index < 0 || index >= n) continue;
02129
02130 RenderTableCell *cell = row->row->at(index);
02131 if (!cell || (int)cell == -1) continue;
02132
02133 #if DEBUG_CARETMODE > 1
02134 kdDebug(6201) << "index " << index << " cell " << cell << endl;
02135 #endif
02136 RenderTable *nestedTable;
02137 if (containsEditableElement(part, cell, nestedTable, fromEnd)) {
02138
02139 if (nestedTable) {
02140 TableRowIterator it(nestedTable, fromEnd);
02141 while (*it) {
02142
02143 cell = findNearestTableCell(part, x, it, fromEnd);
02144 if (cell) break;
02145 if (fromEnd) --it; else ++it;
02146 }
02147 }
02148
02149 return cell;
02150 }
02151 }
02152 return 0;
02153 }
02154
02161 static RenderObject *commonAncestorTableSectionOrCell(RenderObject *r1,
02162 RenderObject *r2)
02163 {
02164 if (!r1 || !r2) return 0;
02165 RenderTableSection *sec = 0;
02166 int start_depth=0, end_depth=0;
02167
02168 RenderObject *n = r1;
02169 while (n->parent()) {
02170 n = n->parent();
02171 start_depth++;
02172 }
02173 n = r2;
02174 while( n->parent()) {
02175 n = n->parent();
02176 end_depth++;
02177 }
02178
02179 while (end_depth > start_depth) {
02180 r2 = r2->parent();
02181 end_depth--;
02182 }
02183 while (start_depth > end_depth) {
02184 r1 = r1->parent();
02185
02186 start_depth--;
02187 }
02188
02189 while (r1 != r2){
02190 r1 = r1->parent();
02191 if (r1->isTableSection()) sec = static_cast<RenderTableSection *>(r1);
02192 r2 = r2->parent();
02193 }
02194
02195
02196
02197 while (r1 && !r1->isTableCell() && !r1->isTableSection() && !r1->isTable())
02198 r1 = r1->parent();
02199
02200 return r1 && r1->isTable() ? sec : r1;
02201 }
02202
02210 static int findRowInSection(RenderTableSection *section, RenderTableCell *cell,
02211 RenderTableSection::RowStruct *&row, RenderTableCell *&directCell)
02212 {
02213
02214 RenderObject *r = cell;
02215 while (r != section) {
02216 if (r->isTableCell()) directCell = static_cast<RenderTableCell *>(r);
02217 r = r->parent();
02218 }
02219
02220
02221
02222
02223 int n = section->numRows();
02224 for (int i = 0; i < n; i++) {
02225 row = §ion->grid[i];
02226
02227
02228 int m = row->row->size();
02229 for (int j = 0; j < m; j++) {
02230 RenderTableCell *c = row->row->at(j);
02231 if (c == directCell) return i;
02232 }
02233
02234 }
02235 Q_ASSERT(false);
02236 return -1;
02237 }
02238
02244 static inline RenderTable *findFirstDescendantTable(RenderObject *leaf, RenderBlock *block)
02245 {
02246 RenderTable *result = 0;
02247 while (leaf && leaf != block) {
02248 if (leaf->isTable()) result = static_cast<RenderTable *>(leaf);
02249 leaf = leaf->parent();
02250 }
02251 return result;
02252 }
02253
02257 static inline RenderTableCell *containingTableCell(RenderObject *r)
02258 {
02259 while (r && !r->isTableCell()) r = r->parent();
02260 return static_cast<RenderTableCell *>(r);
02261 }
02262
02263 inline void ErgonomicEditableLineIterator::calcAndStoreNewLine(
02264 RenderBlock *newBlock, bool toBegin)
02265 {
02266
02267
02268 CaretBoxIterator it;
02269 cbl = CaretBoxLine::constructCaretBoxLine(&lines->cblDeleter,
02270 newBlock, true, toBegin, it);
02271 #if DEBUG_CARETMODE > 3
02272 kdDebug(6201) << cbl->information() << endl;
02273 #endif
02274
02275
02276 if (!cbl) {
02277 return;
02278 }
02279
02280 EditableLineIterator::advance(toBegin);
02281 }
02282
02283 void ErgonomicEditableLineIterator::determineTopologicalElement(
02284 RenderTableCell *oldCell, RenderObject *newObject, bool toBegin)
02285 {
02286
02287
02288
02289
02290
02291 TableRowIterator it;
02292
02293 RenderObject *commonAncestor = commonAncestorTableSectionOrCell(oldCell, newObject);
02294 #if DEBUG_CARETMODE > 1
02295 kdDebug(6201) << " ancestor " << commonAncestor << endl;
02296 #endif
02297
02298
02299 if (!commonAncestor || commonAncestor->isTableCell()) {
02300
02301 RenderTableCell *cell = static_cast<RenderTableCell *>(commonAncestor);
02302 RenderTable *table = findFirstDescendantTable(newObject, cell);
02303
02304 #if DEBUG_CARETMODE > 0
02305 kdDebug(6201) << "table cell: " << cell << endl;
02306 #endif
02307
02308
02309
02310 if (!table) return;
02311
02312 it = TableRowIterator(table, toBegin);
02313
02314 } else if (commonAncestor->isTableSection()) {
02315
02316 RenderTableSection *section = static_cast<RenderTableSection *>(commonAncestor);
02317 RenderTableSection::RowStruct *row;
02318 int idx = findRowInSection(section, oldCell, row, oldCell);
02319 #if DEBUG_CARETMODE > 1
02320 kdDebug(6201) << "table section: row idx " << idx << endl;
02321 #endif
02322
02323 it = TableRowIterator(section, idx);
02324
02325
02326 int rowspan = oldCell->rowSpan();
02327 while (*it && rowspan--) {
02328 if (toBegin) --it; else ++it;
02329 }
02330
02331 } else {
02332 kdError(6201) << "Neither common cell nor section! " << commonAncestor->renderName() << endl;
02333
02334 }
02335
02336 RenderTableCell *cell = findNearestTableCell(lines->m_part, xCoor, it, toBegin);
02337 #if DEBUG_CARETMODE > 1
02338 kdDebug(6201) << "findNearestTableCell result: " << cell << endl;
02339 #endif
02340
02341 RenderBlock *newBlock = cell;
02342 if (!cell) {
02343 Q_ASSERT(commonAncestor->isTableSection());
02344 RenderTableSection *section = static_cast<RenderTableSection *>(commonAncestor);
02345 cell = containingTableCell(section);
02346 #if DEBUG_CARETMODE > 1
02347 kdDebug(6201) << "containing cell: " << cell << endl;
02348 #endif
02349
02350 RenderTable *nestedTable;
02351 bool editableChild = cell && containsEditableChildElement(lines->m_part,
02352 cell, nestedTable, toBegin, section->table());
02353
02354 if (cell && !editableChild) {
02355 #if DEBUG_CARETMODE > 1
02356 kdDebug(6201) << "========= recursive invocation outer =========" << endl;
02357 #endif
02358 determineTopologicalElement(cell, cell->section(), toBegin);
02359 #if DEBUG_CARETMODE > 1
02360 kdDebug(6201) << "========= end recursive invocation outer =========" << endl;
02361 #endif
02362 return;
02363
02364 } else if (cell && nestedTable) {
02365 #if DEBUG_CARETMODE > 1
02366 kdDebug(6201) << "========= recursive invocation inner =========" << endl;
02367 #endif
02368 determineTopologicalElement(cell, nestedTable, toBegin);
02369 #if DEBUG_CARETMODE > 1
02370 kdDebug(6201) << "========= end recursive invocation inner =========" << endl;
02371 #endif
02372 return;
02373
02374 } else {
02375 #if DEBUG_CARETMODE > 1
02376 kdDebug(6201) << "newBlock is table: " << section->table() << endl;
02377 #endif
02378 RenderObject *r = section->table();
02379 int state;
02380 ObjectTraversalState trav = OutsideAscending;
02381 r = advanceSuitableObject(r, trav, toBegin, lines->baseObject(), state);
02382 if (!r) { cbl = 0; return; }
02383
02384 newBlock = static_cast<RenderBlock *>(!r || r->isRenderBlock() ? r : r->containingBlock());
02385 }
02386 #if 0
02387 } else {
02388
02389 newBlock = cell;
02390
02391
02392 if (!toBegin) {
02393 RenderObject *r = newBlock;
02394 int state;
02395 ObjectTraversalState trav = OutsideAscending;
02396 r = advanceSuitableObject(r, trav, true, lines->advancePolicy(), lines->baseObject(), state);
02397 newBlock = static_cast<RenderBlock *>(!r || r->isRenderBlock() ? r : r->containingBlock());
02398 }
02399 #endif
02400 }
02401
02402 calcAndStoreNewLine(newBlock, toBegin);
02403 }
02404
02405 ErgonomicEditableLineIterator &ErgonomicEditableLineIterator::operator ++()
02406 {
02407 RenderTableCell *oldCell = containingTableCell(cbl->enclosingObject());
02408
02409 EditableLineIterator::operator ++();
02410 if (*this == lines->end() || *this == lines->preBegin()) return *this;
02411
02412 RenderTableCell *newCell = containingTableCell(cbl->enclosingObject());
02413
02414 if (!newCell || newCell == oldCell) return *this;
02415
02416 determineTopologicalElement(oldCell, newCell, false);
02417
02418 return *this;
02419 }
02420
02421 ErgonomicEditableLineIterator &ErgonomicEditableLineIterator::operator --()
02422 {
02423 RenderTableCell *oldCell = containingTableCell(cbl->enclosingObject());
02424
02425 EditableLineIterator::operator --();
02426 if (*this == lines->end() || *this == lines->preBegin()) return *this;
02427
02428 RenderTableCell *newCell = containingTableCell(cbl->enclosingObject());
02429
02430 if (!newCell || newCell == oldCell) return *this;
02431
02432 determineTopologicalElement(oldCell, newCell, true);
02433
02434 return *this;
02435 }
02436
02437
02438
02448 static CaretBox *nearestCaretBox(LineIterator &it, CaretViewContext *cv,
02449 int &x, int &absx, int &absy)
02450 {
02451
02452 RenderObject *cb = (*it)->containingBlock();
02453 #if DEBUG_CARETMODE > 4
02454 kdDebug(6200) << "nearestCB: cb " << cb << "@" << (cb ? cb->renderName() : "") << endl;
02455 #endif
02456
02457 if (cb) cb->absolutePosition(absx, absy);
02458 else absx = absy = 0;
02459
02460
02461
02462
02463 x = cv->origX - absx;
02464 CaretBox *caretBox = 0;
02465
02466 int xPos;
02467 int oldXPos = -1;
02468 EditableCaretBoxIterator fbit = it;
02469 #if DEBUG_CARETMODE > 0
02470
02471
02472
02473 #endif
02474
02475 for (CaretBox *b; fbit != (*it)->end(); ++fbit) {
02476 b = *fbit;
02477
02478 #if DEBUG_CARETMODE > 0
02479
02480
02481
02482 #endif
02483 xPos = b->xPos();
02484
02485
02486 if (x < xPos) {
02487
02488 if (oldXPos < 0 || x - (oldXPos + caretBox->width()) > xPos - x) {
02489 caretBox = b;
02490 }
02491 break;
02492 }
02493
02494 caretBox = b;
02495
02496
02497 if (x >= xPos && x < xPos + caretBox->width())
02498 break;
02499 oldXPos = xPos;
02500
02501
02502
02503 }
02504
02505 return caretBox;
02506 }
02507
02513 static void moveItToNextWord(EditableCharacterIterator &it)
02514 {
02515 #if DEBUG_CARETMODE > 0
02516 kdDebug(6200) << "%%%%%%%%%%%%%%%%%%%%% moveItToNextWord" << endl;
02517 #endif
02518 EditableCharacterIterator copy;
02519 while (!it.isEnd() && !(*it).isSpace() && !(*it).isPunct()) {
02520 #if DEBUG_CARETMODE > 2
02521 kdDebug(6200) << "reading1 '" << (*it).latin1() << "'" << endl;
02522 #endif
02523 copy = it;
02524 ++it;
02525 }
02526
02527 if (it.isEnd()) {
02528 it = copy;
02529 return;
02530 }
02531
02532 while (!it.isEnd() && ((*it).isSpace() || (*it).isPunct())) {
02533 #if DEBUG_CARETMODE > 2
02534 kdDebug(6200) << "reading2 '" << (*it).latin1() << "'" << endl;
02535 #endif
02536 copy = it;
02537 ++it;
02538 }
02539
02540 if (it.isEnd()) it = copy;
02541 }
02542
02548 static void moveItToPrevWord(EditableCharacterIterator &it)
02549 {
02550 if (it.isEnd()) return;
02551
02552 #if DEBUG_CARETMODE > 0
02553 kdDebug(6200) << "%%%%%%%%%%%%%%%%%%%%% moveItToPrevWord" << endl;
02554 #endif
02555 EditableCharacterIterator copy;
02556
02557
02558 do {
02559 copy = it;
02560 --it;
02561 #if DEBUG_CARETMODE > 2
02562 if (!it.isEnd()) kdDebug(6200) << "reading1 '" << (*it).latin1() << "'" << endl;
02563 #endif
02564 } while (!it.isEnd() && ((*it).isSpace() || (*it).isPunct()));
02565
02566 if (it.isEnd()) {
02567 it = copy;
02568 return;
02569 }
02570
02571 do {
02572 copy = it;
02573 --it;
02574 #if DEBUG_CARETMODE > 0
02575 if (!it.isEnd()) kdDebug(6200) << "reading2 '" << (*it).latin1() << "' (" << (int)(*it).latin1() << ") box " << it.caretBox() << endl;
02576 #endif
02577 } while (!it.isEnd() && !(*it).isSpace() && !(*it).isPunct());
02578
02579 it = copy;
02580 #if DEBUG_CARETMODE > 1
02581 if (!it.isEnd()) kdDebug(6200) << "effective '" << (*it).latin1() << "' (" << (int)(*it).latin1() << ") box " << it.caretBox() << endl;
02582 #endif
02583 }
02584
02592 static void moveIteratorByPage(LinearDocument &ld,
02593 ErgonomicEditableLineIterator &it, int mindist, bool next)
02594 {
02595
02596
02597 if (it == ld.end() || it == ld.preBegin()) return;
02598
02599 ErgonomicEditableLineIterator copy = it;
02600 #if DEBUG_CARETMODE > 0
02601 kdDebug(6200) << " mindist: " << mindist << endl;
02602 #endif
02603
02604 CaretBoxLine *cbl = *copy;
02605 int absx = 0, absy = 0;
02606
02607 RenderBlock *lastcb = cbl->containingBlock();
02608 Q_ASSERT(lastcb->isRenderBlock());
02609 lastcb->absolutePosition(absx, absy, false);
02610
02611 int lastfby = cbl->begin().data()->yPos();
02612 int lastheight = 0;
02613 int rescue = 1000;
02614 do {
02615 if (next) ++copy; else --copy;
02616 if (copy == ld.end() || copy == ld.preBegin()) break;
02617
02618 cbl = *copy;
02619 RenderBlock *cb = cbl->containingBlock();
02620
02621 int diff = 0;
02622
02623
02624 int fby = cbl->begin().data()->yPos();
02625 if (cb != lastcb) {
02626 if (next) {
02627 diff = absy + lastfby + lastheight;
02628 cb->absolutePosition(absx, absy, false);
02629 diff = absy - diff + fby;
02630 lastfby = 0;
02631 } else {
02632 diff = absy;
02633 cb->absolutePosition(absx, absy, false);
02634 diff -= absy + fby + lastheight;
02635 lastfby = fby - lastheight;
02636 }
02637 #if DEBUG_CARETMODE > 2
02638 kdDebug(6200) << "absdiff " << diff << endl;
02639 #endif
02640 } else {
02641 diff = kAbs(fby - lastfby);
02642 }
02643 #if DEBUG_CARETMODE > 2
02644 kdDebug(6200) << "cbl->begin().data()->yPos(): " << fby << " diff " << diff << endl;
02645 #endif
02646
02647 mindist -= diff;
02648
02649 lastheight = kAbs(fby - lastfby);
02650 lastfby = fby;
02651 lastcb = cb;
02652 it = copy;
02653 #if DEBUG_CARETMODE > 0
02654 kdDebug(6200) << " mindist: " << mindist << endl;
02655 #endif
02656
02657
02658
02659
02660 } while (mindist - lastheight > 0 && --rescue);
02661 }
02662
02663
02664 }