1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
/*
* Dillo Widget
*
* Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "core.hh"
#include <limits.h>
using namespace lout;
namespace dw {
namespace core {
// --------------
// Iterator
// --------------
Iterator::Iterator(Widget *widget, Content::Type mask, bool atEnd)
{
this->widget = widget;
this->mask = mask;
}
Iterator::Iterator(Iterator &it): object::Comparable ()
{
widget = it.widget;
content = it.content;
}
Iterator::~Iterator()
{
}
bool Iterator::equals (Object *other)
{
Iterator *otherIt = (Iterator*)other;
return
this == otherIt ||
(getWidget() == otherIt->getWidget() && compareTo(otherIt) == 0);
}
/**
* \brief Delete the iterator.
*
* The destructor is hidden, implementations may use optimizations for
* the allocation. (Will soon be the case for dw::core::EmptyIteratorFactory.)
*/
void Iterator::unref ()
{
delete this;
}
/**
* \brief Scrolls the viewport, so that the region between \em it1 and
* \em it2 is seen, according to \em hpos and \em vpos.
*
* The parameters \em start and \em end have the same meaning as in
* dw::core::Iterator::getAllocation, \em start refers
* to \em it1, while \em end rerers to \em it2.
*
* If \em it1 and \em it2 point to the same location (see code), only
* \em it1 is regarded, and both belowstart and belowend refer to it.
*/
void Iterator::scrollTo (Iterator *it1, Iterator *it2, int start, int end,
HPosition hpos, VPosition vpos)
{
Allocation alloc1, alloc2, alloc;
int x1, x2, y1, y2;
DeepIterator *eit1, *eit2, *eit3;
int curStart, curEnd, cmp;
bool atStart;
if (it1->equals(it2)) {
it1->getAllocation (start, end, &alloc);
it1->getWidget()->getLayout()->scrollTo (hpos, vpos, alloc.x, alloc.y,
alloc.width,
alloc.ascent + alloc.descent);
} else {
// First, determine the rectangle all iterators from it1 and it2
// allocate, i.e. the smallest rectangle containing all allocations of
// these iterators.
eit1 = new DeepIterator (it1);
eit2 = new DeepIterator (it2);
x1 = INT_MAX;
x2 = INT_MIN;
y1 = INT_MAX;
y2 = INT_MIN;
for (eit3 = (DeepIterator*)eit1->clone (), atStart = true;
(cmp = eit3->compareTo (eit2)) <= 0;
eit3->next (), atStart = false) {
if (atStart)
curStart = start;
else
curStart = 0;
if (cmp == 0)
curEnd = end;
else
curEnd = INT_MAX;
eit3->getAllocation (curStart, curEnd, &alloc);
x1 = misc::min (x1, alloc.x);
x2 = misc::max (x2, alloc.x + alloc.width);
y1 = misc::min (y1, alloc.y);
y2 = misc::max (y2, alloc.y + alloc.ascent + alloc.descent);
}
delete eit3;
delete eit2;
delete eit1;
it1->getAllocation (start, INT_MAX, &alloc1);
it2->getAllocation (0, end, &alloc2);
if (alloc1.x > alloc2.x) {
//
// This is due to a line break within the region. When the line is
// longer than the viewport, and the region is actually quite short,
// the user would not see anything of the region, as in this figure
// (with region marked as "#"):
//
// +----------+ ,-- alloc1
// | | V
// | | ### ###
// ### ### | |
// ^ | | <-- viewport
// | +----------+
// `-- alloc2
// |----------------------------|
// width
//
// Therefore, we make the region smaller, so that the region will be
// displayed like this:
//
// ,-- alloc1
// +----|-----+
// | V |
// | ### ###|
// ### ### | |
// ^ | | <-- viewport
// `-- alloc2 +----------+
// |----------|
// width
//
/** \todo Changes in the viewport size, until the idle function is
* called, are not regarded. */
if (it1->getWidget()->getLayout()->getUsesViewport() &&
x2 - x1 > it1->getWidget()->getLayout()->getWidthViewport()) {
x1 = x2 - it1->getWidget()->getLayout()->getWidthViewport();
x2 = x1 + it1->getWidget()->getLayout()->getWidthViewport();
}
}
if (alloc1.y > alloc2.y) {
// This is similar to the case above, e.g. if the region ends in
// another table column.
if (it1->getWidget()->getLayout()->getUsesViewport() &&
y2 - y1 > it1->getWidget()->getLayout()->getHeightViewport()) {
y1 = y2 - it1->getWidget()->getLayout()->getHeightViewport();
y2 = y1 + it1->getWidget()->getLayout()->getHeightViewport();
}
}
it1->getWidget()->getLayout()->scrollTo (hpos, vpos,
x1, y1, x2 - x1, y2 - y1);
}
}
// -------------------
// EmptyIterator
// -------------------
EmptyIterator::EmptyIterator (Widget *widget, Content::Type mask, bool atEnd):
Iterator (widget, mask, atEnd)
{
this->content.type = (atEnd ? Content::END : Content::START);
}
EmptyIterator::EmptyIterator (EmptyIterator &it): Iterator (it)
{
}
object::Object *EmptyIterator::clone ()
{
return new EmptyIterator (*this);
}
int EmptyIterator::compareTo (object::Comparable *other)
{
EmptyIterator *otherIt = (EmptyIterator*)other;
if (content.type == otherIt->content.type)
return 0;
else if (content.type == Content::START)
return -1;
else
return +1;
}
bool EmptyIterator::next ()
{
content.type = Content::END;
return false;
}
bool EmptyIterator::prev ()
{
content.type = Content::START;
return false;
}
void EmptyIterator::highlight (int start, int end, HighlightLayer layer)
{
}
void EmptyIterator::unhighlight (int direction, HighlightLayer layer)
{
}
void EmptyIterator::getAllocation (int start, int end, Allocation *allocation)
{
}
// ------------------
// TextIterator
// ------------------
TextIterator::TextIterator (Widget *widget, Content::Type mask, bool atEnd,
const char *text): Iterator (widget, mask, atEnd)
{
this->content.type = (atEnd ? Content::END : Content::START);
this->text = (mask & Content::TEXT) ? text : NULL;
}
TextIterator::TextIterator (TextIterator &it): Iterator (it)
{
text = it.text;
}
int TextIterator::compareTo (object::Comparable *other)
{
TextIterator *otherIt = (TextIterator*)other;
if (content.type == otherIt->content.type)
return 0;
switch (content.type) {
case Content::START:
return -1;
case Content::TEXT:
if (otherIt->content.type == Content::START)
return +1;
else
return -1;
case Content::END:
return +1;
default:
misc::assertNotReached();
return 0;
}
}
bool TextIterator::next ()
{
if (content.type == Content::START && text != NULL) {
content.type = Content::TEXT;
content.text = text;
return true;
} else {
content.type = Content::END;
return false;
}
}
bool TextIterator::prev ()
{
if (content.type == Content::END && text != NULL) {
content.type = Content::TEXT;
content.text = text;
return true;
} else {
content.type = Content::START;
return false;
}
}
void TextIterator::getAllocation (int start, int end, Allocation *allocation)
{
// Return the allocation of the widget.
*allocation = *(getWidget()->getAllocation ());
}
// ------------------
// DeepIterator
// ------------------
DeepIterator::Stack::~Stack ()
{
for (int i = 0; i < size (); i++)
get(i)->unref ();
}
/*
* The following two methods are used by dw::core::DeepIterator::DeepIterator,
* when the passed dw::core::Iterator points to a widget. Since a
* dw::core::DeepIterator never returns a widget, the dw::core::Iterator has
* to be corrected, by searching for the next content downwards (within the
* widget pointed to), forwards, and backwards (in the traversed tree).
*/
/*
* Search downwards. If fromEnd is true, start search at the end,
* otherwise at the beginning.
*/
Iterator *DeepIterator::searchDownward (Iterator *it, Content::Type mask,
bool fromEnd)
{
Iterator *it2, *it3;
//DEBUG_MSG (1, "%*smoving down (%swards) from %s\n",
// indent, "", from_end ? "back" : "for", a_Dw_iterator_text (it));
assert (it->getContent()->type == Content::WIDGET);
it2 = it->getContent()->widget->iterator (mask, fromEnd);
if (it2 == NULL) {
// Moving downwards failed.
//DEBUG_MSG (1, "%*smoving down failed\n", indent, "");
return NULL;
}
while (fromEnd ? it2->prev () : it2->next ()) {
//DEBUG_MSG (1, "%*sexamining %s\n",
// indent, "", a_Dw_iterator_text (it2));
if (it2->getContent()->type == Content::WIDGET) {
// Another widget. Search in it downwards.
it3 = searchDownward (it2, mask, fromEnd);
if (it3 != NULL) {
it2->unref ();
return it3;
}
// Else continue in this widget.
} else {
// Success!
//DEBUG_MSG (1, "%*smoving down succeeded: %s\n",
// indent, "", a_Dw_iterator_text (it2));
return it2;
}
}
// Nothing found.
it2->unref ();
//DEBUG_MSG (1, "%*smoving down failed (nothing found)\n", indent, "");
return NULL;
}
/*
* Search sidewards. fromEnd specifies the direction, false means forwards,
* true means backwards.
*/
Iterator *DeepIterator::searchSideward (Iterator *it, Content::Type mask,
bool fromEnd)
{
Iterator *it2, *it3;
//DEBUG_MSG (1, "%*smoving %swards from %s\n",
// indent, "", from_end ? "back" : "for", a_Dw_iterator_text (it));
assert (it->getContent()->type == Content::WIDGET);
it2 = it->cloneIterator ();
while (fromEnd ? it2->prev () : it2->next ()) {
if (it2->getContent()->type == Content::WIDGET) {
// Search downwards in this widget.
it3 = searchDownward (it2, mask, fromEnd);
if (it3 != NULL) {
it2->unref ();
//DEBUG_MSG (1, "%*smoving %swards succeeded: %s\n",
// indent, "", from_end ? "back" : "for",
// a_Dw_iterator_text (it3));
return it3;
}
// Else continue in this widget.
} else {
// Success!
// DEBUG_MSG (1, "%*smoving %swards succeeded: %s\n",
// indent, "", from_end ? "back" : "for",
// a_Dw_iterator_text (it2));
return it2;
}
}
/* Nothing found, go upwards in the tree (if possible). */
it2->unref ();
if (it->getWidget()->getParent ()) {
it2 = it->getWidget()->getParent()->iterator (mask, false);
while (true) {
if (!it2->next ())
misc::assertNotReached ();
if (it2->getContent()->type == Content::WIDGET &&
it2->getContent()->widget == it->getWidget ()) {
it3 = searchSideward (it2, mask, fromEnd);
it2->unref ();
//DEBUG_MSG (1, "%*smoving %swards succeeded: %s\n",
// indent, "", from_end ? "back" : "for",
// a_Dw_iterator_text (it3));
return it3;
}
}
}
// Nothing found at all.
// DEBUG_MSG (1, "%*smoving %swards failed (nothing found)\n",
// indent, "", from_end ? "back" : "for");
return NULL;
}
/**
* \brief Create a new deep iterator from an existing dw::core::Iterator.
*
* The content of the return value will be the content of \em it. If within
* the widget tree, there is no non-widget content, the resulting deep
* iterator is empty (denoted by dw::core::DeepIterator::stack == NULL).
*
* Notes:
*
* <ol>
* <li> The mask of \em i" must include DW_CONTENT_WIDGET, but
* dw::core::DeepIterator::next will never return widgets.
* </ol>
*/
DeepIterator::DeepIterator (Iterator *it)
{
//DEBUG_MSG (1, "a_Dw_ext_iterator_new: %s\n", a_Dw_iterator_text (it));
// Clone input iterator, so the iterator passed as parameter
// remains untouched.
it = it->cloneIterator ();
this->mask = it->getMask ();
hasContents = true;
// If it points to a widget, find a near non-widget content,
// since an DeepIterator should never return widgets.
if (it->getContent()->type == Content::WIDGET) {
Iterator *it2;
// The second argument of searchDownward is actually a matter of
// taste :-)
if ((it2 = searchDownward (it, mask, false)) ||
(it2 = searchSideward (it, mask, false)) ||
(it2 = searchSideward (it, mask, true))) {
it->unref ();
it = it2;
} else {
// This may happen, when a page does not contain any non-widget
// content.
//DEBUG_MSG (1, "a_Dw_ext_iterator_new got totally helpless!\n");
it->unref ();
hasContents = false;
}
}
//DEBUG_MSG (1, " => %s\n", a_Dw_iterator_text (it));
if (hasContents) {
// If this widget has parents, we must construct appropriate iterators.
//
// \todo There may be a faster way instead of iterating through the
// parent widgets.
// Construct the iterators.
int thisLevel = it->getWidget()->getLevel (), level;
Widget *w;
for (w = it->getWidget (), level = thisLevel; w->getParent() != NULL;
w = w->getParent (), level--) {
Iterator *it = w->getParent()->iterator (mask, false);
stack.put (it, level - 1);
while (true) {
bool hasNext = it->next();
assert (hasNext);
if (it->getContent()->type == Content::WIDGET &&
it->getContent()->widget == w)
break;
}
}
stack.put (it, thisLevel);
content = *(it->getContent());
}
}
DeepIterator::~DeepIterator ()
{
}
object::Object *DeepIterator::clone ()
{
DeepIterator *it = new DeepIterator ();
for (int i = 0; i < stack.size (); i++)
it->stack.put (stack.get(i)->cloneIterator (), i);
it->mask = mask;
it->content = content;
it->hasContents = hasContents;
return it;
}
int DeepIterator::compareTo (object::Comparable *other)
{
DeepIterator *otherDeepIterator = (DeepIterator*)other;
// Search the highest level, where the widgets are the same.
int level = 0;
while (stack.get(level)->getWidget ()
== otherDeepIterator->stack.get(level)->getWidget ()) {
if (level == stack.size() - 1 ||
level == otherDeepIterator->stack.size() - 1)
break;
level++;
}
while (stack.get(level)->getWidget ()
!= otherDeepIterator->stack.get(level)->getWidget ())
level--;
return stack.get(level)->compareTo (otherDeepIterator->stack.get(level));
}
DeepIterator *DeepIterator::createVariant(Iterator *it)
{
/** \todo Not yet implemented, and actually not yet needed very much. */
return new DeepIterator (it);
}
bool DeepIterator::isEmpty () {
return !hasContents;
}
/**
* \brief Move iterator forward and store content it.
*
* Returns true on success.
*/
bool DeepIterator::next ()
{
Iterator *it = stack.getTop ();
if (it->next ()) {
if (it->getContent()->type == Content::WIDGET) {
// Widget: new iterator on stack, to search in this widget.
stack.push (it->getContent()->widget->iterator (mask, false));
return next ();
} else {
// Simply return the content of the iterartor.
content = *(it->getContent ());
return true;
}
} else {
// No more data in the top-most widget.
if (stack.size () > 1) {
// Pop iterator from stack, and move to next item in the old one.
stack.pop ();
return next ();
} else {
// Stack is empty.
content.type = Content::END;
return false;
}
}
}
/**
* \brief Move iterator backward and store content it.
*
* Returns true on success.
*/
bool DeepIterator::prev ()
{
Iterator *it = stack.getTop ();
if (it->prev ()) {
if (it->getContent()->type == Content::WIDGET) {
// Widget: new iterator on stack, to search in this widget.
stack.push (it->getContent()->widget->iterator (mask, true));
return prev ();
} else {
// Simply return the content of the iterartor.
content = *(it->getContent ());
return true;
}
} else {
// No more data in the top-most widget.
if (stack.size () > 1) {
// Pop iterator from stack, and move to next item in the old one.
stack.pop ();
return prev ();
} else {
// Stack is empty.
content.type = Content::START;
return false;
}
}
}
// -----------------
// CharIterator
// -----------------
CharIterator::CharIterator ()
{
it = NULL;
}
CharIterator::CharIterator (Widget *widget)
{
Iterator *i = widget->iterator (Content::SELECTION_CONTENT, false);
it = new DeepIterator (i);
i->unref ();
ch = START;
}
CharIterator::~CharIterator ()
{
if (it)
delete it;
}
object::Object *CharIterator::clone()
{
CharIterator *cloned = new CharIterator ();
cloned->it = it->cloneDeepIterator ();
cloned->ch = ch;
cloned->pos = pos;
return cloned;
}
int CharIterator::compareTo(object::Comparable *other)
{
CharIterator *otherIt = (CharIterator*)other;
int c = it->compareTo(otherIt->it);
if (c != 0)
return c;
else
return pos - otherIt->pos;
}
bool CharIterator::next ()
{
if (ch == START || it->getContent()->type == Content::BREAK ||
(it->getContent()->type == Content::TEXT &&
it->getContent()->text[pos] == 0)) {
if (it->next()) {
if (it->getContent()->type == Content::BREAK)
ch = '\n';
else { // if (it->getContent()->type == Content::TEXT)
pos = 0;
ch = it->getContent()->text[pos];
if (ch == 0)
// should not happen, actually
return next ();
}
return true;
}
else {
ch = END;
return false;
}
} else if (ch == END)
return false;
else {
// at this point, it->getContent()->type == Content::TEXT
pos++;
ch = it->getContent()->text[pos];
if (ch == 0) {
if (it->getContent()->space) {
ch = ' ';
} else {
return next ();
}
}
return true;
}
}
bool CharIterator::prev ()
{
if (ch == END || it->getContent()->type == Content::BREAK ||
(it->getContent()->type == Content::TEXT && pos == 0)) {
if (it->prev()) {
if (it->getContent()->type == Content::BREAK)
ch = '\n';
else { // if (it->getContent()->type == Content::TEXT)
if (it->getContent()->text[0] == 0)
return prev ();
else {
pos = strlen (it->getContent()->text);
if (it->getContent()->space) {
ch = ' ';
} else {
pos--;
ch = it->getContent()->text[pos];
}
}
}
return true;
}
else {
ch = START;
return false;
}
} else if (ch == START)
return false;
else {
// at this point, it->getContent()->type == Content::TEXT
pos--;
ch = it->getContent()->text[pos];
return true;
}
}
void CharIterator::highlight (CharIterator *it1, CharIterator *it2,
HighlightLayer layer)
{
if (it2->getChar () == CharIterator::END)
it2->prev ();
if (it1->it->compareTo (it2->it) == 0)
// Only one content => highlight part of it.
it1->it->highlight (it1->pos, it2->pos, layer);
else {
DeepIterator *it = it1->it->cloneDeepIterator ();
int c;
bool start;
for (start = true;
(c = it->compareTo (it2->it)) <= 0;
it->next (), start = false) {
int endOfWord =
it->getContent()->type == Content::TEXT ?
strlen (it->getContent()->text) : 1;
if (start) // first iteration
it->highlight (it1->pos, endOfWord, layer);
else if (c == 0) // last iteration
it->highlight (0, it2->pos, layer);
else
it->highlight (0, endOfWord, layer);
}
delete it;
}
}
void CharIterator::unhighlight (CharIterator *it1, CharIterator *it2,
HighlightLayer layer)
{
if (it1->it->compareTo (it2->it) == 0)
// Only one content => unhighlight it (only for efficiency).
it1->it->unhighlight (0, layer);
else {
DeepIterator *it = it1->it->cloneDeepIterator ();
for (; it->compareTo (it2->it) <= 0; it->next ())
it->unhighlight (-1, layer);
delete it;
}
}
} // namespace core
} // namespace dw
|