1// Aseprite UI Library
2// Copyright (C) 2018 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8
9#ifdef HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#include "ui/link_label.h"
14
15#include "base/launcher.h"
16#include "ui/message.h"
17#include "ui/system.h"
18#include "ui/theme.h"
19
20namespace ui {
21
22LinkLabel::LinkLabel(const std::string& urlOrText)
23 : Label(urlOrText)
24 , m_url(urlOrText)
25{
26 disableFlags(IGNORE_MOUSE);
27 setType(kLinkLabelWidget);
28 initTheme();
29}
30
31LinkLabel::LinkLabel(const std::string& url, const std::string& text)
32 : Label(text)
33 , m_url(url)
34{
35 disableFlags(IGNORE_MOUSE);
36 setType(kLinkLabelWidget);
37 initTheme();
38}
39
40void LinkLabel::setUrl(const std::string& url)
41{
42 m_url = url;
43}
44
45bool LinkLabel::onProcessMessage(Message* msg)
46{
47 switch (msg->type()) {
48
49 case kSetCursorMessage:
50 // TODO theme stuff
51 if (isEnabled() && hasMouseOver()) {
52 set_mouse_cursor(kHandCursor);
53 return true;
54 }
55 break;
56
57 case kMouseEnterMessage:
58 case kMouseLeaveMessage:
59 if (isEnabled()) {
60 if (hasCapture())
61 setSelected(msg->type() == kMouseEnterMessage);
62
63 invalidate(); // TODO theme specific
64 }
65 break;
66
67 case kMouseMoveMessage:
68 if (isEnabled() && hasCapture())
69 setSelected(hasMouseOver());
70 break;
71
72 case kMouseDownMessage:
73 if (isEnabled()) {
74 captureMouse();
75 setSelected(true);
76 }
77 break;
78
79 case kMouseUpMessage:
80 if (hasCapture()) {
81 releaseMouse();
82
83 setSelected(false);
84 invalidate(); // TODO theme specific
85
86 if (hasMouseOver())
87 onClick();
88 }
89 break;
90 }
91
92 return Label::onProcessMessage(msg);
93}
94
95void LinkLabel::onClick()
96{
97 if (!m_url.empty())
98 base::launcher::open_url(m_url);
99
100 Click();
101}
102
103} // namespace ui
104