repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haikudepot / ui_generic / LinkView.cpp
blob1196a6776faa93f77a359ac57f53b629844ccdbb
1 /*
2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
7 #include "LinkView.h"
9 #include <Cursor.h>
10 #include <String.h>
13 LinkView::LinkView(const char* name, const char* string, BMessage* message)
15 BStringView(name, string),
16 BInvoker(message, NULL),
17 fEnabled(true),
18 fMouseInside(false)
20 SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
21 SetExplicitMinSize(BSize(120, B_SIZE_UNSET));
25 void
26 LinkView::AttachedToWindow()
28 _UpdateLinkColor();
32 void
33 LinkView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage)
35 // TODO: Check that no buttons are pressed, don't indicate clickable
36 // link if a button is held.
37 if (transit == B_ENTERED_VIEW) {
38 fMouseInside = true;
39 _UpdateLinkColor();
40 } else if (transit == B_EXITED_VIEW) {
41 fMouseInside = false;
42 _UpdateLinkColor();
47 void
48 LinkView::MouseDown(BPoint where)
50 if (fEnabled)
51 Invoke(Message());
55 void
56 LinkView::Draw(BRect updateRect)
58 if (Text() == NULL)
59 return;
61 SetDrawingMode(B_OP_ALPHA);
63 font_height fontHeight;
64 GetFontHeight(&fontHeight);
66 BRect bounds = Bounds();
68 float y = (bounds.top + bounds.bottom - ceilf(fontHeight.ascent)
69 - ceilf(fontHeight.descent)) / 2.0 + ceilf(fontHeight.ascent);
70 float x = 0.0f;
72 BString text(Text());
73 TruncateString(&text, B_TRUNCATE_END, bounds.Width());
74 DrawString(text, BPoint(x, y));
78 void
79 LinkView::SetEnabled(bool enabled)
81 if (fEnabled != enabled) {
82 fEnabled = enabled;
83 _UpdateLinkColor();
88 void
89 LinkView::MessageReceived(BMessage* message)
91 if (message->what == B_COLORS_UPDATED)
92 _UpdateLinkColor();
94 BStringView::MessageReceived(message);
98 void
99 LinkView::_UpdateLinkColor()
101 BCursorID cursorID = B_CURSOR_ID_SYSTEM_DEFAULT;
103 float tint = B_DARKEN_1_TINT;
104 ViewUIColor(&tint);
106 if (fEnabled) {
107 if (fMouseInside) {
108 cursorID = B_CURSOR_ID_FOLLOW_LINK;
109 SetHighUIColor(B_LINK_HOVER_COLOR, tint);
110 } else
111 SetHighUIColor(B_LINK_TEXT_COLOR, tint);
112 } else
113 SetHighColor(disable_color(ui_color(B_LINK_TEXT_COLOR), ViewColor()));
115 BCursor cursor(cursorID);
116 SetViewCursor(&cursor, true);
117 Invalidate();