From 9c2ab6ed0a7ae8791bd32061b983d6af6ad97815 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 14 Jul 2021 20:08:14 -0500 Subject: [PATCH] (librunview) Selecting URLs --- libs/librunview/RunView.cpp | 137 +++++++++++++++++++++++++++++++++- libs/librunview/RunView.h | 20 +++++ libs/librunview/URLCrunch.cpp | 94 ----------------------- libs/librunview/URLCrunch.h | 40 ---------- 4 files changed, 156 insertions(+), 135 deletions(-) delete mode 100644 libs/librunview/URLCrunch.cpp delete mode 100644 libs/librunview/URLCrunch.h diff --git a/libs/librunview/RunView.cpp b/libs/librunview/RunView.cpp index 0b8ec6f..4497151 100644 --- a/libs/librunview/RunView.cpp +++ b/libs/librunview/RunView.cpp @@ -5,15 +5,22 @@ #include "RunView.h" +#include +#include + RunView::RunView(const char* name) : BTextView(name), - fLastStyled(false) + fLastStyled(false), + fUrlCursor(new BCursor(B_CURSOR_ID_FOLLOW_LINK)), + fMouseDown(false), + fSelecting(false) { MakeEditable(false); SetStylable(true); AdoptSystemColors(); + SetViewCursor(B_CURSOR_SYSTEM_DEFAULT); text_run run = { 0, BFont(), ui_color(B_PANEL_TEXT_COLOR) }; fDefaultRun = { 1, {run} }; @@ -85,3 +92,131 @@ RunView::Insert(const char* text, const text_run_array* runs) lastEnd = urlEnd; } } + + +void +RunView::MouseDown(BPoint where) +{ + uint32 buttons = 0; + Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons); + + if (!(buttons & B_SECONDARY_MOUSE_BUTTON)) + BTextView::MouseDown(where); + + BUrl url; + if ((buttons & B_PRIMARY_MOUSE_BUTTON) && OverUrl(where, &url)) { + fMouseDown = true; + if (url.IsValid() == true) + fLastClicked = url; + } +} + + +void +RunView::MouseUp(BPoint where) +{ + BTextView::MouseUp(where); + + if (fMouseDown && fSelecting == false && fLastClicked.IsValid() == true) { + fLastClicked.OpenWithPreferredApplication(true); + fLastClicked = BUrl(); + } + fMouseDown = false; + fSelecting = false; +} + + +void +RunView::MouseMoved(BPoint where, uint32 code, const BMessage* drag) +{ + if (fSelecting == true) + return; + + if (code == B_INSIDE_VIEW) + if (OverUrl(where) == true) + SetViewCursor(fUrlCursor); + else + SetViewCursor(B_CURSOR_SYSTEM_DEFAULT); +} + + +void +RunView::Select(int32 startOffset, int32 endOffset) +{ + BTextView::Select(startOffset, endOffset); + if (startOffset < endOffset) { + fSelecting = true; + } +} + + +BString +RunView::WordAt(BPoint point) +{ + int32 wordOffset = OffsetAt(point); + int32 lineOffset = OffsetAt(LineAt(point)); + const char* lineBuff = GetLine(LineAt(point)); + BString line(lineBuff); + delete lineBuff; + + int32 wordStart = line.FindLast(" ", wordOffset - lineOffset) + 1; + int32 wordEnd = line.FindFirst(" ", wordOffset - lineOffset); + + if (wordStart == B_ERROR) + wordStart = 0; + if (wordEnd == B_ERROR) + wordEnd = line.CountChars(); + + BString buffer; + line.CopyCharsInto(buffer, wordStart, wordEnd - wordStart); + return buffer; +} + + +const char* +RunView::GetLine(int32 line) +{ + int32 length = 0; + int32 startOffset = OffsetAt(line); + int32 maxLength = TextLength() - startOffset; + while (length < maxLength && ByteAt(startOffset + length) != '\n') + length++; + + char* buffer = new char[length]; + GetText(startOffset, length, buffer); + return buffer; +} + + +bool +RunView::OverText(BPoint where) +{ + int32 offset = OffsetAt(where); + BPoint point = PointAt(offset); + if (point.x + 10 < where.x) + return false; + return true; +} + + +bool +RunView::OverUrl(BPoint where, BUrl* url) +{ + if (OverText(where) == false) + return false; + + BString word = WordAt(where); + if (BUrl(word.String()).IsValid() == true) { + if (url != NULL) + url->SetUrlString(word); + return true; + } + return false; +} + + +void +RunView::ScrollToBottom() +{ + ScrollToOffset(TextLength()); +} diff --git a/libs/librunview/RunView.h b/libs/librunview/RunView.h index 4e5c6ac..8eb14f4 100644 --- a/libs/librunview/RunView.h +++ b/libs/librunview/RunView.h @@ -6,6 +6,7 @@ #define _RUN_VIEW_H #include +#include class RunView : public BTextView { @@ -19,10 +20,29 @@ public: // Only differs in that it changes font face and color of any URLs virtual void Insert(const char* text, const text_run_array* runs = NULL); + virtual void MouseDown(BPoint where); + virtual void MouseUp(BPoint where); + virtual void MouseMoved(BPoint where, uint32 code, const BMessage* drag); + + virtual void Select(int32 startOffset, int32 endOffset); + + BString WordAt(BPoint point); + const char* GetLine(int32 line); + + bool OverText(BPoint where); + bool OverUrl(BPoint where, BUrl* url = NULL); + + void ScrollToBottom(); + private: bool fLastStyled; text_run_array fDefaultRun; text_run_array fUrlRun; + + BCursor* fUrlCursor; + BUrl fLastClicked; + bool fMouseDown; + bool fSelecting; }; #endif // _RUN_VIEW_H diff --git a/libs/librunview/URLCrunch.cpp b/libs/librunview/URLCrunch.cpp deleted file mode 100644 index 40199b4..0000000 --- a/libs/librunview/URLCrunch.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is Vision. - * - * The Initial Developer of the Original Code is The Vision Team. - * Portions created by The Vision Team are - * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights - * Reserved. - * - * Contributor(s): Rene Gollent - * Todd Lair - */ - -#include - -#include "URLCrunch.h" - -URLCrunch::URLCrunch (const char* data, int32 len) - : buffer (""), - current_pos (0) -{ - buffer.Append (data, len); -} - -URLCrunch::~URLCrunch (void) -{ -} - -int32 -URLCrunch::Crunch (BString* url) -{ - if (current_pos >= buffer.Length()) - return B_ERROR; - - const int32 tagNum = 7; - const char* tags[tagNum] = { - "http://", - "https://", - "www.", - "ftp://", - "ftp.", - "file:/", - "mailto:" - }; - - int32 marker (buffer.Length()); - int32 pos (current_pos); - int32 url_length (0); - int32 markers[tagNum]; - int32 i(0); - - for (i = 0; i < tagNum; ++i) - markers[i] = buffer.IFindFirst (tags[i], pos); - - for (i = 0; i < tagNum; ++i) - - if (markers[i] != B_ERROR - && markers[i] < marker) { - url_length = markers[i] + strlen(tags[i]); - - url_length += strcspn (buffer.String() + url_length, " \t\n|\\<>\")(][}{;'*^"); - - - int len (strlen (tags[i])); - - if (url_length - markers[i] > len - && (isdigit (buffer[markers[i] + len]) - || isalpha (buffer[markers[i] + len]))) { - marker = markers[i]; - pos = url_length + 1; - url_length -= marker; - } else - pos = markers[i] + 1; - } - - if (marker < buffer.Length()) { - *url = ""; - - url->Append (buffer.String() + marker, url_length); - } - - current_pos = pos; - - return marker < buffer.Length() ? marker : B_ERROR; -} diff --git a/libs/librunview/URLCrunch.h b/libs/librunview/URLCrunch.h deleted file mode 100644 index 7129137..0000000 --- a/libs/librunview/URLCrunch.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is Vision. - * - * The Initial Developer of the Original Code is The Vision Team. - * Portions created by The Vision Team are - * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights - * Reserved. - * - * Contributor(s): Todd Lair - * - */ - -#ifndef URLCRUNCH_H_ -#define URLCRUNCH_H_ - -#include - -class URLCrunch -{ - BString buffer; - int32 current_pos; - -public: - - URLCrunch (const char*, int32); - ~URLCrunch (void); - int32 Crunch (BString*); -}; - -#endif