-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaDescriptionView.cpp
More file actions
163 lines (134 loc) · 3.14 KB
/
Copy pathMediaDescriptionView.cpp
File metadata and controls
163 lines (134 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "MediaDescriptionView.h"
#include <Application.h>
#include <InterfaceDefs.h>
#include <Message.h>
#include <Rect.h>
#include <Region.h>
#include <Roster.h>
#include <Window.h>
namespace {
bool
IsPrimaryMouseClick(BMessage* message)
{
int32 buttons = 0;
if (!message || message->FindInt32("buttons", &buttons) != B_OK)
return true;
return (buttons & B_PRIMARY_MOUSE_BUTTON) != 0
&& (buttons & (B_SECONDARY_MOUSE_BUTTON
| B_TERTIARY_MOUSE_BUTTON)) == 0;
}
bool
StayedNear(BPoint first, BPoint second)
{
const float x = first.x - second.x;
const float y = first.y - second.y;
return x * x + y * y <= 9.0f;
}
} // namespace
MediaDescriptionView::MediaDescriptionView(const char* name)
:
BTextView(name)
{
MakeEditable(false);
MakeSelectable(true);
SetWordWrap(true);
SetInsets(4, 4, 4, 4);
}
void
MediaDescriptionView::AttachedToWindow()
{
BTextView::AttachedToWindow();
_UpdateTextRect();
}
void
MediaDescriptionView::FrameResized(float width, float height)
{
BTextView::FrameResized(width, height);
_UpdateTextRect();
if (fResetOnNextResize) {
_ResetToTop();
fResetOnNextResize = false;
}
}
void
MediaDescriptionView::MouseDown(BPoint where)
{
const MediaDescriptionLink* link = _LinkAt(where);
fPendingLink = link != nullptr && IsPrimaryMouseClick(
Window() ? Window()->CurrentMessage() : nullptr);
fPendingLinkUrl = fPendingLink ? link->url : "";
fPendingLinkPoint = where;
fResetOnNextResize = false;
BTextView::MouseDown(where);
}
void
MediaDescriptionView::MouseUp(BPoint where)
{
BTextView::MouseUp(where);
int32 selectionStart = 0;
int32 selectionEnd = 0;
GetSelection(&selectionStart, &selectionEnd);
if (fPendingLink && selectionStart == selectionEnd
&& StayedNear(fPendingLinkPoint, where) && be_roster) {
const char* argv[] = {fPendingLinkUrl.c_str(), nullptr};
be_roster->Launch("text/html", 1, argv);
}
fPendingLink = false;
fPendingLinkUrl.clear();
}
void
MediaDescriptionView::SetLinks(const std::vector<MediaDescriptionLink>& links)
{
fLinks = links;
_ResetToTop();
fResetOnNextResize = true;
}
void
MediaDescriptionView::Reflow()
{
SetWordWrap(true);
_UpdateTextRect();
_ResetToTop();
fResetOnNextResize = true;
Invalidate();
}
void
MediaDescriptionView::_ResetToTop()
{
ScrollTo(0.0f, 0.0f);
fPendingLink = false;
fPendingLinkUrl.clear();
}
void
MediaDescriptionView::_UpdateTextRect()
{
float left;
float top;
float right;
float bottom;
GetInsets(&left, &top, &right, &bottom);
BRect bounds = Bounds();
BRect textRect = TextRect();
textRect.left = left;
textRect.top = top;
textRect.right = bounds.Width() - right;
float visibleBottom = bounds.Height() - bottom;
if (textRect.bottom < visibleBottom)
textRect.bottom = visibleBottom;
if (textRect.right < textRect.left)
textRect.right = textRect.left;
if (textRect.bottom < textRect.top)
textRect.bottom = textRect.top;
SetTextRect(textRect);
}
const MediaDescriptionLink*
MediaDescriptionView::_LinkAt(BPoint where) const
{
for (const MediaDescriptionLink& link : fLinks) {
BRegion linkRegion;
GetTextRegion(link.start, link.end, &linkRegion);
if (linkRegion.Contains(where))
return &link;
}
return nullptr;
}