2 * Copyright 2005-2012, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
8 * Copyright 1999, Be Incorporated. All Rights Reserved.
9 * This file may be used under the terms of the Be Sample Code License.
13 #include "AttributeTextControl.h"
22 #include <StorageKit.h>
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "People"
28 #define B_URL_MIME "application/x-vnd.Be.URL."
30 AttributeTextControl::AttributeTextControl(const char* label
,
31 const char* attribute
)
33 BTextControl(NULL
, "", NULL
),
34 fAttribute(attribute
),
37 if (label
!= NULL
&& label
[0] != 0) {
38 SetLabel(BString(B_TRANSLATE("%attribute_label:"))
39 .ReplaceFirst("%attribute_label", label
));
41 SetAlignment(B_ALIGN_RIGHT
, B_ALIGN_LEFT
);
45 AttributeTextControl::~AttributeTextControl()
50 AttributeTextControl::MouseDown(BPoint mousePosition
)
52 if(_VisibleLabelBounds().Contains(mousePosition
) && _ContainsUrl())
53 _HandleLabelClicked(Text());
55 BTextControl::MouseDown(mousePosition
);
60 AttributeTextControl::MouseMoved(BPoint mousePosition
, uint32 transit
, const BMessage
* dragMessage
)
62 if (_VisibleLabelBounds().Contains(mousePosition
) && _ContainsUrl()) {
63 BCursor
linkCursor(B_CURSOR_ID_FOLLOW_LINK
);
64 SetViewCursor(&linkCursor
, true);
66 SetViewCursor(B_CURSOR_SYSTEM_DEFAULT
, true);
68 BTextControl::MouseMoved(mousePosition
, transit
, dragMessage
);
73 AttributeTextControl::HasChanged()
75 return fOriginalValue
!= Text();
80 AttributeTextControl::Revert()
83 SetText(fOriginalValue
);
88 AttributeTextControl::Update()
90 fOriginalValue
= Text();
95 AttributeTextControl::_HandleLabelClicked(const char *text
)
97 BString
argument(text
);
99 if (Attribute() == "META:url") {
100 _MakeUniformUrl(argument
);
102 BString mimeType
= B_URL_MIME
;
103 _BuildMimeString(mimeType
, argument
);
105 if (mimeType
!= "") {
106 const char *args
[] = {argument
.String(), 0};
107 be_roster
->Launch(mimeType
.String(), 1, const_cast<char**>(args
));
109 } else if (Attribute() == "META:email") {
110 if (argument
.IFindFirst("mailto:") != 0 && argument
!= "")
111 argument
.Prepend("mailto:");
113 // TODO: Could check for possible e-mail patterns.
114 if (argument
!= "") {
115 const char *args
[] = {argument
.String(), 0};
116 be_roster
->Launch("text/x-email", 1, const_cast<char**>(args
));
123 AttributeTextControl::_MakeUniformUrl(BString
&url
) const
125 if (url
.StartsWith("www"))
126 url
.Prepend("http://");
127 else if (url
.StartsWith("ftp."))
128 url
.Prepend("ftp://");
135 AttributeTextControl::_BuildMimeString(BString
&mimeType
, const BString
&url
)
138 if (url
.IFindFirst("http://") == 0 || url
.IFindFirst("ftp://") == 0
139 || url
.IFindFirst("https://") || url
.IFindFirst("gopher://") == 0) {
141 mimeType
.Append(url
, url
.FindFirst(':'));
144 if (!BMimeType::IsValid(mimeType
.String()))
152 AttributeTextControl::_ContainsUrl() const
154 BString
argument(Text());
156 if (Attribute() == "META:url") {
157 BString mimeType
= B_URL_MIME
;
158 if (_BuildMimeString(mimeType
, _MakeUniformUrl(argument
)) != "")
161 else if (Attribute() == "META:email") {
171 AttributeTextControl::_VisibleLabelBounds() const
173 // TODO: Could actually check current alignment of the label.
174 // The code below works only for right aligned labels.
175 BRect
bounds(Bounds());
176 bounds
.right
= Divider();
177 bounds
.left
= bounds
.right
- StringWidth(Label());