UrlTextView.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
  3. * All rights reserved. Distributed under the terms of the MIT license.
  4. */
  5. #ifndef _URL_TEXT_VIEW_H
  6. #define _URL_TEXT_VIEW_H
  7. #include <TextView.h>
  8. #include <Url.h>
  9. class BPopUpMenu;
  10. /*! TextView that highlights and allows selecting of URLs― also provides a
  11. * right-click pop-up menu with internet search options */
  12. class UrlTextView : public BTextView {
  13. public:
  14. UrlTextView(const char* name);
  15. UrlTextView(const char* name, const BFont* initialFont,
  16. const rgb_color* initialColor, uint32 flags);
  17. virtual void MessageReceived(BMessage* msg);
  18. virtual void MouseDown(BPoint where);
  19. virtual void MouseUp(BPoint where);
  20. virtual void MouseMoved(BPoint where, uint32 code, const BMessage* drag);
  21. virtual void Select(int32 startOffset, int32 endOffset);
  22. // Only differs in that it changes font face and color of any URLs
  23. void Insert(const char* text, const text_run_array* runs = NULL);
  24. void SetText(const char* text, const text_run_array* runs = NULL);
  25. BString WordAt(BPoint point);
  26. void FindWordAround(int32 offset, int32* start, int32* end,
  27. BString* _word = NULL);
  28. const char* GetLine(int32 line);
  29. BUrl UrlAt(BPoint point);
  30. bool OverText(BPoint where);
  31. bool OverUrl(BPoint where);
  32. private:
  33. BPopUpMenu* _RightClickPopUp(BPoint where);
  34. bool _FindUrlString(BString text, int32* start, int32* end,
  35. int32 offset);
  36. // Checks if char is allowed in a url, as per rfc3986
  37. bool _IsValidUrlChar(char c);
  38. // For safe-keeping
  39. text_run_array fNormalRun;
  40. text_run_array fUrlRun;
  41. BCursor* fUrlCursor;
  42. // Information between MouseDown and MouseUp
  43. BUrl fLastClicked;
  44. bool fMouseDown;
  45. bool fSelecting;
  46. };
  47. #endif // _URL_TEXT_VIEW_H