-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtooltip.cpp
135 lines (105 loc) · 3.59 KB
/
tooltip.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "tooltip.hpp"
#include "widget.hpp"
namespace firefly
{
void tooltip::init(base_window& base)
{
SetRectEmpty(&this->rc_client);
this->bound_handle = NULL;
this->set_font(this->create_font("Arial", 17));
this->set_foreground(base.foreground());
this->set_background(base.background());
}
tooltip::tooltip(window& parent_window)
: message_controller(parent_window.instance(), parent_window.handle(), false)
{
this->init(parent_window);
this->set_default_tooltip_handlers();
}
tooltip::tooltip(widget& parent_widget)
: message_controller(parent_widget.instance(), parent_widget.handle(), false)
{
this->init(parent_widget);
this->set_default_tooltip_handlers();
}
tooltip::~tooltip()
{
}
bool tooltip::create()
{
this->set_handle(CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, this->parent_handle(), NULL, this->instance(), NULL));
if (!this->handle())
return false;
this->reset_font();
return this->initialize_widget(this->handle());
}
bool tooltip::bind_control(HWND handle, std::string const& caption)
{
this->bound_handle = handle;
TOOLINFO tool_info;
memset(&tool_info, 0, sizeof(TOOLINFO));
tool_info.cbSize = sizeof(TOOLINFO);
tool_info.hwnd = this->parent_handle();
tool_info.uFlags = TTF_IDISHWND | TTF_SUBCLASS | TTF_TRANSPARENT; // TTF_CENTERTIP
tool_info.uId = reinterpret_cast<UINT_PTR>(this->bound_handle);
tool_info.lpszText = const_cast<LPSTR>(caption.c_str());
return (SendMessage(this->handle(), TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&tool_info)) != FALSE) &&
(SendMessage(this->handle(), TTM_SETMAXTIPWIDTH, 0, SHORT_MAX) != FALSE);
}
void tooltip::reset_font()
{
SendMessage(this->handle(), WM_SETFONT, reinterpret_cast<WPARAM>(this->font()), TRUE);
}
std::string tooltip::get_text()
{
char text_buffer[2048];
memset(text_buffer, 0, sizeof(text_buffer));
TOOLINFO tool_info;
memset(&tool_info, 0, sizeof(TOOLINFO));
tool_info.cbSize = sizeof(TOOLINFO);
tool_info.hwnd = this->parent_handle();
tool_info.uId = reinterpret_cast<UINT_PTR>(this->bound_handle);
tool_info.lpszText = text_buffer;
SendMessage(this->handle(), TTM_GETTEXT, sizeof(text_buffer), reinterpret_cast<LPARAM>(&tool_info));
return std::string(text_buffer);
}
void tooltip::set_default_tooltip_handlers()
{
this->add_message_handler(CUSTOM_NOTIFY, [this](HWND hWnd, WPARAM wParam, LPARAM lParam) -> LRESULT
{
NMHDR* custom_notify = reinterpret_cast<NMHDR*>(lParam);
switch (custom_notify->code)
{
case TTN_SHOW:
return this->on_show_tooltip(custom_notify);
case NM_CUSTOMDRAW:
return this->try_custom_draw(reinterpret_cast<NMTTCUSTOMDRAW*>(lParam));
default:
break;
}
return 0;
});
}
LRESULT tooltip::on_show_tooltip(NMHDR* custom_notify)
{
GetClientRect(custom_notify->hwndFrom, &this->rc_client);
return 0;
}
LRESULT tooltip::try_custom_draw(NMTTCUSTOMDRAW* custom_draw)
{
if (custom_draw->nmcd.dwDrawStage == CDDS_PREPAINT)
{
if (!IsRectEmpty(&this->rc_client))
{
this->reset_graphics(rectangle(this->rc_client.left, this->rc_client.top, this->rc_client.right - this->rc_client.left, this->rc_client.bottom - this->rc_client.top));
this->draw_rectangle(&this->rc_client, RGB(64, 64, 64));
custom_draw->nmcd.rc.top -= 2;
this->draw_text(this->get_text(), &custom_draw->nmcd.rc, this->foreground(), DT_CENTER);
this->commit_drawing(custom_draw->nmcd.hdc, &this->rc_client);
return CDRF_SKIPDEFAULT;
}
}
return CDRF_DODEFAULT;
}
}