1/* 2 * Copyright 2014, Stephan A��mus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 7#include "LinkView.h" 8 9#include <Cursor.h> 10#include <String.h> 11 12 13LinkView::LinkView(const char* name, const char* string, BMessage* message) 14 : 15 BStringView(name, string), 16 BInvoker(message, NULL), 17 fEnabled(true), 18 fMouseInside(false) 19{ 20 SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 21 SetExplicitMinSize(BSize(120, B_SIZE_UNSET)); 22} 23 24 25void 26LinkView::AttachedToWindow() 27{ 28 _UpdateLinkColor(); 29} 30 31 32void 33LinkView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage) 34{ 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(); 43 } 44} 45 46 47void 48LinkView::MouseDown(BPoint where) 49{ 50 if (fEnabled) 51 Invoke(Message()); 52} 53 54 55void 56LinkView::Draw(BRect updateRect) 57{ 58 if (Text() == NULL) 59 return; 60 61 SetDrawingMode(B_OP_ALPHA); 62 63 font_height fontHeight; 64 GetFontHeight(&fontHeight); 65 66 BRect bounds = Bounds(); 67 68 float y = (bounds.top + bounds.bottom - ceilf(fontHeight.ascent) 69 - ceilf(fontHeight.descent)) / 2.0 + ceilf(fontHeight.ascent); 70 float x = 0.0f; 71 72 BString text(Text()); 73 TruncateString(&text, B_TRUNCATE_END, bounds.Width()); 74 DrawString(text, BPoint(x, y)); 75} 76 77 78void 79LinkView::SetEnabled(bool enabled) 80{ 81 if (fEnabled != enabled) { 82 fEnabled = enabled; 83 _UpdateLinkColor(); 84 } 85} 86 87 88void 89LinkView::MessageReceived(BMessage* message) 90{ 91 if (message->what == B_COLORS_UPDATED) 92 _UpdateLinkColor(); 93 94 BStringView::MessageReceived(message); 95} 96 97 98void 99LinkView::_UpdateLinkColor() 100{ 101 BCursorID cursorID = B_CURSOR_ID_SYSTEM_DEFAULT; 102 103 float tint = B_DARKEN_1_TINT; 104 ViewUIColor(&tint); 105 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())); 114 115 BCursor cursor(cursorID); 116 SetViewCursor(&cursor, true); 117 Invalidate(); 118} 119