dillo  3.0.5
About: dillo is a small, fast, extensible Web browser particularly suitable for older or smaller computers and embedded systems (but only limited or no support for frames, CSS, JavaScript, Java).
  Fossies Dox: dillo-3.0.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

textblock_iterator.cc
Go to the documentation of this file.
1 /*
2  * Dillo Widget
3  *
4  * Copyright 2005-2007, 2012-2013 Sebastian Geerken <sgeerken@dillo.org>
5  *
6  * (Parts of this file were originally part of textblock.cc.)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "textblock.hh"
23 #include "../lout/msg.h"
24 #include "../lout/misc.hh"
25 
26 #include <stdio.h>
27 #include <math.h>
28 
29 using namespace lout;
30 
31 namespace dw {
32 
33 Textblock::TextblockIterator::TextblockIterator (Textblock *textblock,
35  bool atEnd):
36  core::Iterator (textblock, mask, atEnd)
37 {
38  index = atEnd ? textblock->words->size () : -1;
40 }
41 
44  int index):
45  core::Iterator (textblock, mask, false)
46 {
47  this->index = index;
48 
49  if (index < 0)
51  else if (index >= textblock->words->size ())
53  else
54  content = textblock->words->getRef(index)->content;
55 }
56 
58 {
59  return new TextblockIterator ((Textblock*)getWidget(), getMask(), index);
60 }
61 
63 {
64  return index - ((TextblockIterator*)other)->index;
65 }
66 
68 {
69  Textblock *textblock = (Textblock*)getWidget();
70 
71  if (content.type == core::Content::END)
72  return false;
73 
74  do {
75  index++;
76  if (index >= textblock->words->size ()) {
77  content.type = core::Content::END;
78  return false;
79  }
80  } while ((textblock->words->getRef(index)->content.type & getMask()) == 0);
81 
82  content = textblock->words->getRef(index)->content;
83  return true;
84 }
85 
87 {
88  Textblock *textblock = (Textblock*)getWidget();
89 
90  if (content.type == core::Content::START)
91  return false;
92 
93  do {
94  index--;
95  if (index < 0) {
96  content.type = core::Content::START;
97  return false;
98  }
99  } while ((textblock->words->getRef(index)->content.type & getMask()) == 0);
100 
101  content = textblock->words->getRef(index)->content;
102  return true;
103 }
104 
106  core::HighlightLayer layer)
107 {
108  Textblock *textblock = (Textblock*)getWidget();
109  int index1 = index, index2 = index;
110 
111  int oldStartIndex = textblock->hlStart[layer].index;
112  int oldStartChar = textblock->hlStart[layer].nChar;
113  int oldEndIndex = textblock->hlEnd[layer].index;
114  int oldEndChar = textblock->hlEnd[layer].nChar;
115 
116  if (textblock->hlStart[layer].index > textblock->hlEnd[layer].index) {
117  /* nothing is highlighted */
118  textblock->hlStart[layer].index = index;
119  textblock->hlEnd[layer].index = index;
120  }
121 
122  if (textblock->hlStart[layer].index >= index) {
123  index2 = textblock->hlStart[layer].index;
124  textblock->hlStart[layer].index = index;
125  textblock->hlStart[layer].nChar = start;
126  }
127 
128  if (textblock->hlEnd[layer].index <= index) {
129  index2 = textblock->hlEnd[layer].index;
130  textblock->hlEnd[layer].index = index;
131  textblock->hlEnd[layer].nChar = end;
132  }
133 
134  if (oldStartIndex != textblock->hlStart[layer].index ||
135  oldStartChar != textblock->hlStart[layer].nChar ||
136  oldEndIndex != textblock->hlEnd[layer].index ||
137  oldEndChar != textblock->hlEnd[layer].nChar)
138  textblock->queueDrawRange (index1, index2);
139 }
140 
142  core::HighlightLayer layer)
143 {
144  Textblock *textblock = (Textblock*)getWidget();
145  int index1 = index, index2 = index;
146 
147  if (textblock->hlStart[layer].index > textblock->hlEnd[layer].index)
148  return;
149 
150  int oldStartIndex = textblock->hlStart[layer].index;
151  int oldStartChar = textblock->hlStart[layer].nChar;
152  int oldEndIndex = textblock->hlEnd[layer].index;
153  int oldEndChar = textblock->hlEnd[layer].nChar;
154 
155  if (direction == 0) {
156  index1 = textblock->hlStart[layer].index;
157  index2 = textblock->hlEnd[layer].index;
158  textblock->hlStart[layer].index = 1;
159  textblock->hlEnd[layer].index = 0;
160  } else if (direction > 0 && textblock->hlStart[layer].index <= index) {
161  index1 = textblock->hlStart[layer].index;
162  textblock->hlStart[layer].index = index + 1;
163  textblock->hlStart[layer].nChar = 0;
164  } else if (direction < 0 && textblock->hlEnd[layer].index >= index) {
165  index1 = textblock->hlEnd[layer].index;
166  textblock->hlEnd[layer].index = index - 1;
167  textblock->hlEnd[layer].nChar = INT_MAX;
168  }
169 
170  if (oldStartIndex != textblock->hlStart[layer].index ||
171  oldStartChar != textblock->hlStart[layer].nChar ||
172  oldEndIndex != textblock->hlEnd[layer].index ||
173  oldEndChar != textblock->hlEnd[layer].nChar)
174  textblock->queueDrawRange (index1, index2);
175 }
176 
179 {
180  Textblock *textblock = (Textblock*)getWidget();
181  int lineIndex = textblock->findLineOfWord (index);
182  Line *line = textblock->lines->getRef (lineIndex);
183  Word *word = textblock->words->getRef (index);
184 
185  allocation->x =
186  textblock->allocation.x + textblock->lineXOffsetWidget (line);
187 
188  for (int i = line->firstWord; i < index; i++) {
189  Word *w = textblock->words->getRef(i);
190  allocation->x += w->size.width + w->effSpace;
191  }
192  if (start > 0 && word->content.type == core::Content::TEXT) {
193  allocation->x += textblock->textWidth (word->content.text, 0, start,
194  word->style,
195  word->flags & Word::WORD_START,
196  (word->flags & Word::WORD_END)
197  && word->content.text[start] == 0);
198  }
199  allocation->y = textblock->lineYOffsetCanvas (line) + line->boxAscent -
200  word->size.ascent;
201 
202  allocation->width = word->size.width;
203  if (word->content.type == core::Content::TEXT) {
204  int wordEnd = strlen(word->content.text);
205 
206  if (start > 0 || end < wordEnd) {
207  end = misc::min(end, wordEnd); /* end could be INT_MAX */
208  allocation->width =
209  textblock->textWidth (word->content.text, start, end - start,
210  word->style,
211  (word->flags & Word::WORD_START)
212  && start == 0,
213  (word->flags & Word::WORD_END)
214  && word->content.text[end] == 0);
215  }
216  }
217  allocation->ascent = word->size.ascent;
218  allocation->descent = word->size.descent;
219 }
220 
221 } // namespace dw
dw::Textblock::index
int index
Definition: textblock.hh:490
dw::Textblock::TextblockIterator::next
bool next()
Move iterator forward and store content it.
Definition: textblock_iterator.cc:67
dw::core::Allocation::y
int y
Definition: types.hh:166
dw::core::Allocation::x
int x
Definition: types.hh:165
dw::core::HighlightLayer
HighlightLayer
Definition: types.hh:42
dw::Textblock
A Widget for rendering text blocks, i.e. paragraphs or sequences of paragraphs.
Definition: textblock.hh:149
dw::core::Requisition::descent
int descent
Definition: types.hh:176
dw::core::Content::Type
Type
Definition: types.hh:187
dw::Textblock::Line::firstWord
int firstWord
Definition: textblock.hh:308
dw::Textblock::TextblockIterator::clone
lout::object::Object * clone()
Return an exact copy of the object.
Definition: textblock_iterator.cc:57
dw::core::Allocation
Represents the allocation, i.e. actual position and size of a dw::core::Widget.
Definition: types.hh:163
dw::core::Content::text
const char * text
Definition: types.hh:204
dw::Textblock::Word::WORD_START
Definition: textblock.hh:351
dw::Textblock::findLineOfWord
int findLineOfWord(int wordIndex)
Find the line of word wordIndex.
Definition: textblock.cc:1341
dw::Textblock::Word
Definition: textblock.hh:328
lout::misc::min
T min(T a, T b)
Definition: misc.hh:19
textblock.hh
dw::core::Allocation::descent
int descent
Definition: types.hh:169
dw::Textblock::queueDrawRange
void queueDrawRange(int index1, int index2)
Definition: textblock.cc:2374
dw::Textblock::textWidth
int textWidth(const char *text, int start, int len, core::style::Style *style, bool isStart, bool isEnd)
Definition: textblock.cc:1559
dw::Textblock::TextblockIterator::index
int index
Definition: textblock.hh:412
dw::Textblock::Word::style
core::style::Style * style
Definition: textblock.hh:388
lout::object::Object
This is the base class for many other classes, which defines very common virtual methods.
Definition: object.hh:24
dw::Textblock::TextblockIterator
Definition: textblock.hh:409
dw::Textblock::Word::content
core::Content content
Definition: textblock.hh:371
dw::Textblock::nChar
int nChar
Definition: textblock.hh:490
dw::Textblock::lineXOffsetWidget
int lineXOffsetWidget(Line *line)
Like lineXOffset, but relative to the allocation (i.e. including border etc.).
Definition: textblock.hh:562
dw::core::Requisition::ascent
int ascent
Definition: types.hh:175
lout
Definition: container.cc:26
dw::Textblock::lines
lout::misc::SimpleVector< Line > * lines
Definition: textblock.hh:484
dw::Textblock::Line
Definition: textblock.hh:306
dw::core::Requisition::width
int width
Definition: types.hh:174
dw::Textblock::TextblockIterator::compareTo
int compareTo(lout::object::Comparable *other)
Compare two objects c1 and c2.
Definition: textblock_iterator.cc:62
dw::Textblock::Word::flags
short flags
Definition: textblock.hh:370
dw::Textblock::TextblockIterator::TextblockIterator
TextblockIterator(Textblock *textblock, core::Content::Type mask, bool atEnd)
Definition: textblock_iterator.cc:33
dw::Textblock::Word::effSpace
short effSpace
Definition: textblock.hh:362
dw::Textblock::TextblockIterator::prev
bool prev()
Move iterator backward and store content it.
Definition: textblock_iterator.cc:86
dw::core::Iterator::content
Content content
Definition: iterator.hh:26
dw::Textblock::Word::size
core::Requisition size
Definition: textblock.hh:359
dw::Textblock::lineYOffsetCanvas
int lineYOffsetCanvas(Line *line)
Definition: textblock.hh:590
dw::Textblock::words
lout::misc::NotSoSimpleVector< Word > * words
Definition: textblock.hh:487
dw::core::Content::END
Definition: types.hh:189
dw::core::Content::TEXT
Definition: types.hh:190
dw::Textblock::Line::boxAscent
int boxAscent
Definition: textblock.hh:313
dw::core::Allocation::ascent
int ascent
Definition: types.hh:168
lout::object::Comparable
Instances of a sub class of may be compared (less, greater).
Definition: object.hh:41
dw::Textblock::hlEnd
struct dw::Textblock::@23 hlEnd[core::HIGHLIGHT_NUM_LAYERS]
dw::Textblock::hlStart
struct dw::Textblock::@23 hlStart[core::HIGHLIGHT_NUM_LAYERS]
dw::Textblock::Word::WORD_END
Definition: textblock.hh:355
dw::core::Widget::allocation
Allocation allocation
The current allocation: size and position, always relative to the canvas.
Definition: widget.hh:151
dw
Dw is in this namespace, or sub namespaces of this one.
Definition: alignedtextblock.cc:26
dw::core::Content::type
short type
Definition: types.hh:201
dw::Textblock::TextblockIterator::getAllocation
void getAllocation(int start, int end, core::Allocation *allocation)
Return the shape, which a part of the item, the iterator points on, allocates.
Definition: textblock_iterator.cc:177
dw::Textblock::TextblockIterator::highlight
void highlight(int start, int end, core::HighlightLayer layer)
Extend highlighted region to contain part of the current content.
Definition: textblock_iterator.cc:105
dw::Textblock::TextblockIterator
friend class TextblockIterator
Definition: textblock.hh:430
dw::core::Allocation::width
int width
Definition: types.hh:167
dw::core::Content::START
Definition: types.hh:188
dw::Textblock::TextblockIterator::unhighlight
void unhighlight(int direction, core::HighlightLayer layer)
Shrink highlighted region to no longer contain the current content.
Definition: textblock_iterator.cc:141