-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColoredToolTip.cpp
226 lines (190 loc) · 5.71 KB
/
ColoredToolTip.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ColoredToolTip.cpp : implementation file
//
#include "stdafx.h"
#include "SCIPicEditor.h"
#include "ColoredToolTip.h"
// CColoredToolTip
IMPLEMENT_DYNAMIC(CColoredToolTip, CWnd)
CColoredToolTip::CColoredToolTip()
{
SetColor(GetSysColor(COLOR_INFOBK));
// Get a nice font (menu font) to use.
NONCLIENTMETRICS metrics = { 0 };
metrics.cbSize = sizeof(metrics);
if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0))
{
m_font.CreateFontIndirect(&metrics.lfMenuFont);
metrics.lfMenuFont.lfWeight = 700; // Bold
m_fontBold.CreateFontIndirect(&metrics.lfMenuFont);
}
_nFormatFlags = 0;
_hBitmapWeak = NULL;
}
CColoredToolTip::~CColoredToolTip()
{
}
void CColoredToolTip::SetColor(COLORREF cr)
{
_cr = cr;
_brush.DeleteObject();
_brush.CreateSolidBrush(_cr);
}
#define TOOLTIP_MARGINS 4
#define TOOLTIP_OFFSET_Y 10 // should really be a drawtext
#define LEFT_MARGIN 2
#define TOPBOTTOM_MARGIN 1
#define Y_OFFSET 3
void CColoredToolTip::Show(CPoint ptScreen, std::string pszText)
{
if (_strOne != pszText)
{
Invalidate(FALSE); // make sure to repaint when text updates.
_strOne = pszText;
}
_strTwo = TEXT("");
_strThree = TEXT("");
_nFormatFlags = 0;
// Show the static!
CDC *pDC = GetDC();
if (pDC)
{
CRect rc(0, 0, 0, 0);
HGDIOBJ hOld = pDC->SelectObject(&m_font);
pDC->DrawText(_strOne.c_str(), &rc, DT_CALCRECT);
pDC->SelectObject(hOld);
rc.OffsetRect(ptScreen.x, ptScreen.y);
SetWindowPos(NULL, rc.left, rc.top + TOOLTIP_OFFSET_Y, rc.Width() + TOOLTIP_MARGINS, rc.Height() + TOOLTIP_MARGINS, SWP_NOZORDER | SWP_NOACTIVATE);
ShowWindow(SW_SHOWNOACTIVATE);
ReleaseDC(pDC);
}
}
void CColoredToolTip::Show(CPoint ptScreen, HBITMAP hBitmap, CSize size)
{
_hBitmapWeak = hBitmap;
_size = size;
if (!IsWindowVisible())
{
CDC *pDC = GetDC();
if (pDC)
{
// Add some margins
SetWindowPos(NULL, ptScreen.x, ptScreen.y - Y_OFFSET, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE);
ShowWindow(SW_SHOWNOACTIVATE);
ReleaseDC(pDC);
}
}
else
{
SetWindowPos(NULL, ptScreen.x, ptScreen.y - Y_OFFSET, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
Invalidate();
}
}
void CColoredToolTip::Show(CPoint pt, std::string strMethod, const std::vector<std::string> &strParams, size_t cParams)
{
_strOne = strMethod + "(";
_strTwo = "";
_strThree = "";
_nFormatFlags = DT_SINGLELINE;
BOOL fBeyondLast = (cParams >= strParams.size());
for (size_t i = 0; i < strParams.size(); i++)
{
BOOL fLast = (i == strParams.size()- 1);
std::string *pStrAddTo;
if (fLast && fBeyondLast)
{
pStrAddTo = &_strTwo;
}
else if (i < cParams)
{
pStrAddTo = &_strOne;
}
else if (i == cParams)
{
pStrAddTo = &_strTwo;
}
else
{
pStrAddTo = &_strThree;
}
*pStrAddTo += strParams[i];
if (i < (strParams.size() - 1))
{
*pStrAddTo += " ";
}
}
_strThree += ")";
if (!IsWindowVisible())
{
CDC *pDC = GetDC();
if (pDC)
{
// Calculate size.
CSize size;
CRect rc;
rc.SetRectEmpty();
pDC->DrawText(_strOne.c_str(), -1, &rc, DT_CALCRECT | _nFormatFlags);
size.cy = rc.Height();
size.cx = rc.Width();
rc.SetRectEmpty();
pDC->DrawText(_strTwo.c_str(), -1, &rc, DT_CALCRECT | _nFormatFlags);
size.cx += rc.Width() * 5 / 4; // Because it's bold
rc.SetRectEmpty();
pDC->DrawText(_strThree.c_str(), -1, &rc, DT_CALCRECT | _nFormatFlags);
size.cx += rc.Width();
// Add some margins
size.cy += TOPBOTTOM_MARGIN * 2;
size.cx += LEFT_MARGIN * 2;
SetWindowPos(NULL, pt.x, pt.y - TOPBOTTOM_MARGIN - Y_OFFSET, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE);
ShowWindow(SW_SHOWNOACTIVATE);
ReleaseDC(pDC);
}
}
else
{
Invalidate();
}
}
void CColoredToolTip::Hide()
{
ShowWindow(SW_HIDE);
}
BEGIN_MESSAGE_MAP(CColoredToolTip, CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CColoredToolTip::OnPaint()
{
// standard paint routine
RECT rcClient;
GetClientRect(&rcClient);
CPaintDC dc(this);
if (_hBitmapWeak)
{
// We have a bitmap
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
HGDIOBJ hOld = dcMem.SelectObject(_hBitmapWeak);
dc.BitBlt(0, 0, _size.cx, _size.cy, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(hOld);
}
else
{
// Just some text
HGDIOBJ hOld = dc.SelectObject(&m_font);
dc.FillSolidRect(&rcClient, _cr);
rcClient.left += LEFT_MARGIN;
rcClient.top += TOPBOTTOM_MARGIN;
COLORREF crTextOld = dc.SetTextColor(GetSysColor(COLOR_INFOTEXT));
CRect rcSegment;
dc.DrawText(_strOne.c_str(), -1, &rcSegment, _nFormatFlags | DT_CALCRECT);
dc.DrawText(_strOne.c_str(), -1, &rcClient, _nFormatFlags);
dc.SetTextColor(crTextOld);
rcClient.left += rcSegment.Width();
dc.SelectObject(&m_fontBold);
dc.DrawText(_strTwo.c_str(), -1, &rcSegment, _nFormatFlags | DT_CALCRECT);
dc.DrawText(_strTwo.c_str(), -1, &rcClient, _nFormatFlags);
dc.SelectObject(&m_font);
rcClient.left += rcSegment.Width();
dc.DrawText(_strThree.c_str(), -1, &rcClient, _nFormatFlags);
dc.SelectObject(hOld);
}
}