diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-27 13:59:45 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-27 13:59:45 +0300 |
commit | 1496a2a027770f0b50bbb7bbcb0c7f11f022b906 (patch) | |
tree | dfd3b988add4cf287d14c81a14e0c0a76e9b228e /src/ui/documentwidget.c | |
parent | c15e1e3126862306244a42d8b058231acb407309 (diff) |
DocumentWidget: Marking a selection with the mouse
Diffstat (limited to 'src/ui/documentwidget.c')
-rw-r--r-- | src/ui/documentwidget.c | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 9c6fe718..bc1b8e66 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c | |||
@@ -112,6 +112,10 @@ static iRect documentBounds_DocumentWidget_(const iDocumentWidget *d) { | |||
112 | return rect; | 112 | return rect; |
113 | } | 113 | } |
114 | 114 | ||
115 | static iInt2 documentPos_DocumentWidget_(const iDocumentWidget *d, iInt2 pos) { | ||
116 | return addY_I2(sub_I2(pos, topLeft_Rect(documentBounds_DocumentWidget_(d))), d->scrollY); | ||
117 | } | ||
118 | |||
115 | static void requestUpdated_DocumentWidget_(iAnyObject *obj) { | 119 | static void requestUpdated_DocumentWidget_(iAnyObject *obj) { |
116 | iDocumentWidget *d = obj; | 120 | iDocumentWidget *d = obj; |
117 | const int wasPending = exchange_Atomic(&d->isSourcePending, iTrue); | 121 | const int wasPending = exchange_Atomic(&d->isSourcePending, iTrue); |
@@ -368,6 +372,10 @@ const iString *valueString_Command(const char *cmd, const char *label) { | |||
368 | return collect_String(newCStr_String(suffixPtr_Command(cmd, label))); | 372 | return collect_String(newCStr_String(suffixPtr_Command(cmd, label))); |
369 | } | 373 | } |
370 | 374 | ||
375 | static const char *sourceLoc_DocumentWidget_(const iDocumentWidget *d, iInt2 pos) { | ||
376 | return findLoc_GmDocument(d->doc, documentPos_DocumentWidget_(d, pos)); | ||
377 | } | ||
378 | |||
371 | static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *ev) { | 379 | static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *ev) { |
372 | iWidget *w = as_Widget(d); | 380 | iWidget *w = as_Widget(d); |
373 | if (isResize_UserEvent(ev)) { | 381 | if (isResize_UserEvent(ev)) { |
@@ -441,15 +449,15 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e | |||
441 | } | 449 | } |
442 | else { | 450 | else { |
443 | const iBool wrap = d->foundMark.start != NULL; | 451 | const iBool wrap = d->foundMark.start != NULL; |
444 | d->foundMark = finder( | 452 | d->foundMark = finder(d->doc, text_InputWidget(find), dir > 0 ? d->foundMark.end |
445 | d->doc, text_InputWidget(find), dir > 0 ? d->foundMark.end : d->foundMark.start); | 453 | : d->foundMark.start); |
446 | if (!d->foundMark.start && wrap) { | 454 | if (!d->foundMark.start && wrap) { |
447 | /* Wrap around. */ | 455 | /* Wrap around. */ |
448 | d->foundMark = finder(d->doc, text_InputWidget(find), NULL); | 456 | d->foundMark = finder(d->doc, text_InputWidget(find), NULL); |
449 | } | 457 | } |
450 | if (d->foundMark.start) { | 458 | if (d->foundMark.start) { |
451 | const iGmRun *found; | 459 | const iGmRun *found; |
452 | if ((found = findRunCStr_GmDocument(d->doc, d->foundMark.start)) != NULL) { | 460 | if ((found = findRunAtLoc_GmDocument(d->doc, d->foundMark.start)) != NULL) { |
453 | scrollTo_DocumentWidget_(d, mid_Rect(found->bounds).y); | 461 | scrollTo_DocumentWidget_(d, mid_Rect(found->bounds).y); |
454 | } | 462 | } |
455 | } | 463 | } |
@@ -530,24 +538,42 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e | |||
530 | } | 538 | } |
531 | processContextMenuEvent_Widget(d->menu, ev); | 539 | processContextMenuEvent_Widget(d->menu, ev); |
532 | switch (processEvent_Click(&d->click, ev)) { | 540 | switch (processEvent_Click(&d->click, ev)) { |
533 | case finished_ClickResult: | ||
534 | if (d->hoverLink) { | ||
535 | iAssert(d->hoverLink->linkId); | ||
536 | postCommandf_App("open url:%s", | ||
537 | cstr_String(absoluteUrl_DocumentWidget_( | ||
538 | d, linkUrl_GmDocument(d->doc, d->hoverLink->linkId)))); | ||
539 | } | ||
540 | return iTrue; | ||
541 | case started_ClickResult: | 541 | case started_ClickResult: |
542 | d->selecting = iFalse; | 542 | d->selecting = iFalse; |
543 | return iTrue; | 543 | return iTrue; |
544 | case double_ClickResult: | 544 | case drag_ClickResult: { |
545 | case drag_ClickResult: | ||
546 | /* Begin selecting a range of text. */ | 545 | /* Begin selecting a range of text. */ |
547 | d->selectMark = iNullRange; | 546 | if (!d->selecting) { |
548 | d->selecting = iTrue; | 547 | d->selecting = iTrue; |
548 | d->selectMark.start = d->selectMark.end = | ||
549 | sourceLoc_DocumentWidget_(d, d->click.startPos); | ||
550 | refresh_Widget(w); | ||
551 | } | ||
552 | const char *loc = sourceLoc_DocumentWidget_(d, pos_Click(&d->click)); | ||
553 | if (!d->selectMark.start) { | ||
554 | d->selectMark.start = d->selectMark.end = loc; | ||
555 | } | ||
556 | else if (loc) { | ||
557 | d->selectMark.end = loc; | ||
558 | } | ||
549 | refresh_Widget(w); | 559 | refresh_Widget(w); |
550 | return iTrue; | 560 | return iTrue; |
561 | } | ||
562 | case finished_ClickResult: | ||
563 | if (!isMoved_Click(&d->click)) { | ||
564 | if (d->hoverLink) { | ||
565 | iAssert(d->hoverLink->linkId); | ||
566 | postCommandf_App("open url:%s", | ||
567 | cstr_String(absoluteUrl_DocumentWidget_( | ||
568 | d, linkUrl_GmDocument(d->doc, d->hoverLink->linkId)))); | ||
569 | } | ||
570 | if (d->selectMark.start) { | ||
571 | d->selectMark = iNullRange; | ||
572 | refresh_Widget(w); | ||
573 | } | ||
574 | } | ||
575 | return iTrue; | ||
576 | case double_ClickResult: | ||
551 | case aborted_ClickResult: | 577 | case aborted_ClickResult: |
552 | return iTrue; | 578 | return iTrue; |
553 | default: | 579 | default: |
@@ -568,6 +594,10 @@ struct Impl_DrawContext { | |||
568 | 594 | ||
569 | static void fillRange_DrawContext_(iDrawContext *d, const iGmRun *run, enum iColorId color, | 595 | static void fillRange_DrawContext_(iDrawContext *d, const iGmRun *run, enum iColorId color, |
570 | iRangecc mark, iBool *isInside) { | 596 | iRangecc mark, iBool *isInside) { |
597 | if (mark.start > mark.end) { | ||
598 | /* Selection may be done in either direction. */ | ||
599 | iSwap(const char *, mark.start, mark.end); | ||
600 | } | ||
571 | if ((!*isInside && contains_Range(&run->text, mark.start)) || *isInside) { | 601 | if ((!*isInside && contains_Range(&run->text, mark.start)) || *isInside) { |
572 | int x = 0; | 602 | int x = 0; |
573 | if (!*isInside) { | 603 | if (!*isInside) { |
@@ -582,6 +612,9 @@ static void fillRange_DrawContext_(iDrawContext *d, const iGmRun *run, enum iCol | |||
582 | else { | 612 | else { |
583 | *isInside = iTrue; /* at least until the next run */ | 613 | *isInside = iTrue; /* at least until the next run */ |
584 | } | 614 | } |
615 | if (w > width_Rect(run->visBounds) - x) { | ||
616 | w = width_Rect(run->visBounds) - x; | ||
617 | } | ||
585 | const iInt2 visPos = add_I2(run->bounds.pos, addY_I2(d->bounds.pos, -d->widget->scrollY)); | 618 | const iInt2 visPos = add_I2(run->bounds.pos, addY_I2(d->bounds.pos, -d->widget->scrollY)); |
586 | fillRect_Paint(&d->paint, (iRect){ addX_I2(visPos, x), | 619 | fillRect_Paint(&d->paint, (iRect){ addX_I2(visPos, x), |
587 | init_I2(w, height_Rect(run->bounds)) }, color); | 620 | init_I2(w, height_Rect(run->bounds)) }, color); |
@@ -620,6 +653,8 @@ static void drawRun_DrawContext_(void *context, const iGmRun *run) { | |||
620 | fillRange_DrawContext_(d, run, brown_ColorId, d->widget->selectMark, &d->inSelectMark); | 653 | fillRange_DrawContext_(d, run, brown_ColorId, d->widget->selectMark, &d->inSelectMark); |
621 | drawString_Text(run->font, visPos, run->color, &text); | 654 | drawString_Text(run->font, visPos, run->color, &text); |
622 | deinit_String(&text); | 655 | deinit_String(&text); |
656 | |||
657 | // drawRect_Paint(&d->paint, (iRect){ visPos, run->bounds.size }, red_ColorId); | ||
623 | } | 658 | } |
624 | 659 | ||
625 | static void draw_DocumentWidget_(const iDocumentWidget *d) { | 660 | static void draw_DocumentWidget_(const iDocumentWidget *d) { |