23 #include "../lout/debug.hh" 32 extern "C" char *
strndup(
const char *s,
size_t size)
34 char *r = (
char *) malloc (size + 1);
47 SelectionState::SelectionState ()
53 selectionState = NONE;
57 linkState = LINK_NONE;
61 SelectionState::~SelectionState ()
67 void SelectionState::reset ()
73 void SelectionState::resetSelection ()
81 selectionState = NONE;
85 void SelectionState::resetLink ()
90 linkState = LINK_NONE;
93 bool SelectionState::buttonPress (
Iterator *it,
int charPos,
int linkNo,
99 if (!event)
return ret;
103 layout->emitLinkPress (itWidget, linkNo, -1, -1, -1, event);
105 }
else if (linkNo != -1) {
107 (void) layout->emitLinkPress (itWidget, linkNo, -1, -1, -1, event);
109 linkState = LINK_PRESSED;
110 linkButton =
event->button;
121 linkChar = correctCharPos (link, charPos);
128 }
else if (event->
button == 1) {
130 highlight (
false, 0);
133 selectionState = SELECTING;
140 fromChar = correctCharPos (from, charPos);
141 to = from->cloneDeepIterator ();
142 toChar = correctCharPos (to, charPos);
150 bool SelectionState::buttonRelease (
Iterator *it,
int charPos,
int linkNo,
156 if (linkState == LINK_PRESSED && event && event->
button == linkButton) {
160 (void) layout->emitLinkRelease (itWidget, linkNo, -1, -1, -1, event);
163 if (linkNo == linkNumber) {
165 (void) layout->emitLinkClick (itWidget, linkNo, -1, -1, -1, event);
171 switchLinkToSelection (it, charPos);
175 if (selectionState == SELECTING && event && event->
button == 1) {
178 adjustSelection (it, charPos);
180 if (from->compareTo (to) == 0 && fromChar == toChar)
185 selectionState = SELECTED;
192 bool SelectionState::buttonMotion (
Iterator *it,
int charPos,
int linkNo,
195 if (linkState == LINK_PRESSED) {
197 if (linkNo != linkNumber)
200 switchLinkToSelection (it, charPos);
202 }
else if (selectionState == SELECTING) {
204 adjustSelection (it, charPos);
216 int charPos,
int linkNo,
221 return buttonPress (it, charPos, linkNo, (
EventButton*)event);
224 return buttonRelease (it, charPos, linkNo, (
EventButton*)event);
227 return buttonMotion (it, charPos, linkNo, (
EventMotion*)event);
242 void SelectionState::switchLinkToSelection (
Iterator *it,
int charPos)
247 highlight (
false, 0);
251 from = link->cloneDeepIterator ();
253 to = from->createVariant (it);
254 toChar = correctCharPos (to, charPos);
255 selectionState = SELECTING;
275 void SelectionState::adjustSelection (
Iterator *it,
int charPos)
278 int newToChar, cmpOld, cmpNew, cmpDiff, len;
279 bool bruteHighlighting =
false;
282 newToChar = correctCharPos (newTo, charPos);
284 cmpOld = to->compareTo (from);
287 if (cmpOld == 0 || cmpNew == 0) {
290 bruteHighlighting =
true;
291 }
else if (cmpOld * cmpNew < 0) {
294 bruteHighlighting =
true;
299 if (cmpOld * cmpDiff > 0) {
302 len = correctCharPos (to, END_OF_WORD);
303 highlight0 (
true, newTo, newToChar, to, len + 1, 1);
305 highlight0 (
true, to, 0, newTo, newToChar, -1);
308 if (cmpOld * cmpDiff < 0) {
310 highlight0 (
false, to, 0, newTo, 0, cmpDiff);
316 len = correctCharPos (newTo, END_OF_WORD);
323 if (bruteHighlighting)
324 highlight (
false, 0);
330 if (bruteHighlighting)
351 void SelectionState::highlight0 (
bool fl,
DeepIterator *from,
int fromChar,
355 int cmp, aChar, bChar;
362 if (fromChar < toChar)
385 i->
next (), start =
false) {
391 }
else if (cmp == 0) {
407 void SelectionState::copy()
412 int cmp, aChar, bChar;
417 cmp = from->compareTo (to);
419 if (from->getContent()->type == Content::TEXT) {
420 si = from->getTopIterator ();
421 if (fromChar < toChar)
444 i->
next (), start =
false) {
452 }
else if (cmp == 0) {
479 layout->copySelection(strbuf.
getChars());