-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickableLabelView.cpp
More file actions
113 lines (101 loc) · 3.07 KB
/
Copy pathClickableLabelView.cpp
File metadata and controls
113 lines (101 loc) · 3.07 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
#include "ClickableLabelView.h"
#include "Messages.h"
#include <Cursor.h>
#include <Font.h>
#include <InterfaceDefs.h>
#include <Messenger.h>
#include <Message.h>
#include <String.h>
#include <View.h>
#include <Window.h>
#include <math.h>
ClickableLabelView::ClickableLabelView(const char* name, uint32 msgWhat)
: BView(name, B_WILL_DRAW), fMsgWhat(msgWhat)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
void ClickableLabelView::SetText(const char* text) {
fText = text ? text : "";
InvalidateLayout();
Invalidate();
}
void ClickableLabelView::UpdateColors() {
rgb_color bg = ui_color(B_PANEL_BACKGROUND_COLOR);
SetViewColor(bg);
SetLowColor(bg);
Invalidate();
}
void ClickableLabelView::AttachedToWindow() {
BView::AttachedToWindow();
UpdateColors();
}
void ClickableLabelView::Draw(BRect updateRect) {
SetHighColor(ViewColor());
FillRect(updateRect);
font_height fh;
GetFontHeight(&fh);
SetHighColor(ui_color(B_PANEL_TEXT_COLOR));
BString text(fText.c_str());
BFont font;
GetFont(&font);
font.TruncateString(&text, B_TRUNCATE_END, Bounds().Width());
float textHeight = fh.ascent + fh.descent;
float y = floorf((Bounds().Height() - textHeight) / 2.0f + fh.ascent);
DrawString(text.String(), BPoint(0.0f, y));
}
void ClickableLabelView::MouseDown(BPoint where) {
BMessage* current = Window() ? Window()->CurrentMessage() : nullptr;
int32 buttons = 0;
if (current)
current->FindInt32("buttons", &buttons);
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
BView* parent = Parent();
if (parent) {
BPoint screenWhere = where;
ConvertToScreen(&screenWhere);
BMessage msg(MSG_SHOW_REPLICANT_MENU);
msg.AddPoint("screen_where", screenWhere);
BMessenger(parent).SendMessage(&msg);
}
return;
}
if (fMsgWhat && !fText.empty()) {
BView* parent = Parent();
if (parent) {
BMessage msg(fMsgWhat);
BMessenger(parent).SendMessage(&msg);
}
BView::MouseDown(BPoint());
return;
}
BView::MouseDown(where);
}
void ClickableLabelView::MouseMoved(BPoint, uint32 transit, const BMessage*) {
if (transit == B_ENTERED_VIEW && fMsgWhat && !fText.empty()) {
BCursor c(B_CURSOR_ID_FOLLOW_LINK);
SetViewCursor(&c);
} else if (transit == B_EXITED_VIEW) {
BCursor c(B_CURSOR_ID_SYSTEM_DEFAULT);
SetViewCursor(&c);
}
}
BSize ClickableLabelView::MinSize() {
font_height fh;
GetFontHeight(&fh);
float w = fText.empty() ? 0.0f : StringWidth(fText.c_str());
if (w > 24.0f)
w = 24.0f;
return BSize(w, fh.ascent + fh.descent);
}
BSize ClickableLabelView::PreferredSize() {
font_height fh;
GetFontHeight(&fh);
float w = fText.empty() ? 0.0f : StringWidth(fText.c_str());
if (w > 260.0f) w = 260.0f;
return BSize(w, fh.ascent + fh.descent);
}
BSize ClickableLabelView::MaxSize() {
font_height fh;
GetFontHeight(&fh);
return BSize(B_SIZE_UNLIMITED, fh.ascent + fh.descent);
}