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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
#include "main.h"
#include "./listWidgetInternal.h"
#include <strsafe.h>
#define TOOLTIP_MARGIN_LEFT_DLU 3
#define TOOLTIP_MARGIN_TOP_DLU 1
#define TOOLTIP_MARGIN_RIGHT_DLU 3
#define TOOLTIP_MARGIN_BOTTOM_DLU 1
#define TOOLTIP_DELAY_INITIAL 1000
#define TOOLTIP_DELAY_RESHOW 400
static ATOM LISTWIDGETTOOLTIP_PROP = 0;
static LRESULT WINAPI
ListWidgetTooltip_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
typedef struct ListWidgetTooltip
{
HWND window;
HWND owner;
BOOL active;
POINT position;
wchar_t *buffer;
ListWidgetItem *item;
ListWidgetItemPart part;
RECT partRect;
BOOL blockLocationChange;
WNDPROC originalProc;
DWORD showTime;
} ListWidgetTooltip;
static HWND
ListWidget_TooltipCreateWindow(HWND owner, WNDPROC *originalProc, HANDLE windowProperty)
{
HWND hwnd;
TOOLINFO ti;
hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT | WS_EX_LAYERED,
TOOLTIPS_CLASS, NULL, WS_CLIPSIBLINGS | WS_POPUP | TTS_NOANIMATE | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
owner, NULL, NULL, NULL);
if ( hwnd == NULL )
return NULL;
MLSkinWindow2(Plugin_GetLibraryWindow(), hwnd, SKINNEDWND_TYPE_TOOLTIP,
SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS);
if (NULL != originalProc)
{
if (0 == LISTWIDGETTOOLTIP_PROP)
LISTWIDGETTOOLTIP_PROP = GlobalAddAtom(TEXT("ListWidgetTooltipProp"));
if (0 != LISTWIDGETTOOLTIP_PROP)
{
*originalProc = (WNDPROC)(LONG_PTR)SetWindowLongPtr(hwnd, GWLP_WNDPROC,
(LONGX86)(LONG_PTR)ListWidgetTooltip_WindowProc);
if (NULL != *originalProc &&
FALSE == SetProp(hwnd, MAKEINTATOM(LISTWIDGETTOOLTIP_PROP), windowProperty))
{
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)*originalProc);
*originalProc = NULL;
}
}
else
*originalProc = NULL;
}
SendMessage(hwnd, CCM_SETVERSION, 6, 0L);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
ZeroMemory(&ti, sizeof(ti));
ti.cbSize = sizeof(ti);
ti.hwnd = owner;
ti.lpszText = LPSTR_TEXTCALLBACK;
ti.uFlags = 0/*TTF_TRACK | TTF_ABSOLUTE*/;
SendMessage(hwnd, TTM_ADDTOOL, 0, (LPARAM)&ti);
return hwnd;
}
ListWidgetTooltip*
ListWidget_TooltipCreate(HWND hwnd)
{
ListWidgetTooltip *tooltip;
tooltip = (ListWidgetTooltip*)malloc(sizeof(ListWidgetTooltip));
if (NULL == tooltip)
return FALSE;
ZeroMemory(tooltip, sizeof(ListWidgetTooltip));
tooltip->window = ListWidget_TooltipCreateWindow(hwnd, &tooltip->originalProc, tooltip);
if (NULL == tooltip->window)
{
ListWidget_TooltipDestroy(tooltip);
return NULL;
}
tooltip->owner = hwnd;
ListWidget_TooltipFontChanged(tooltip);
return tooltip;
}
void
ListWidget_TooltipDestroy(ListWidgetTooltip *tooltip)
{
if (NULL != tooltip)
{
if (NULL != tooltip->window)
DestroyWindow(tooltip->window);
String_Free(tooltip->buffer);
free(tooltip);
}
}
void
ListWidget_TooltipFontChanged(ListWidgetTooltip *tooltip)
{
WidgetStyle *style;
RECT marginsRect;
HDC hdc;
long maxWidth;
if (NULL == tooltip)
return;
if (NULL == tooltip->window)
{
// attempt to recover...
tooltip->window = ListWidget_TooltipCreateWindow(tooltip->owner, &tooltip->originalProc, tooltip);
if (NULL == tooltip->window)
return;
tooltip->active = FALSE;
tooltip->part = ListWidgetItemPart_None;
SetRectEmpty(&tooltip->partRect);
}
style = WIDGET_GET_STYLE(tooltip->owner);
if (NULL == style)
return;
hdc = GetDCEx(tooltip->window, NULL, DCX_CACHE | DCX_NORESETATTRS);
if (NULL != hdc)
{
HFONT font = (HFONT)SendMessage(tooltip->window, WM_GETFONT, 0, 0L);
HFONT prevFont = SelectFont(hdc, font);
maxWidth = Graphics_GetAveStrWidth(hdc, 36);
SelectFont(hdc, prevFont);
ReleaseDC(tooltip->window, hdc);
}
else
maxWidth = 100;
SendMessage(tooltip->window, TTM_SETMAXTIPWIDTH, 0, (LPARAM)maxWidth);
WIDGETSTYLE_DLU_TO_HORZ_PX_MIN(marginsRect.left, style, TOOLTIP_MARGIN_LEFT_DLU, 3);
WIDGETSTYLE_DLU_TO_VERT_PX_MIN(marginsRect.top, style, TOOLTIP_MARGIN_TOP_DLU, 2);
WIDGETSTYLE_DLU_TO_HORZ_PX_MIN(marginsRect.right, style, TOOLTIP_MARGIN_RIGHT_DLU, 3);
WIDGETSTYLE_DLU_TO_VERT_PX_MIN(marginsRect.bottom, style, TOOLTIP_MARGIN_BOTTOM_DLU, 2);
SendMessage(tooltip->window, TTM_SETMARGIN, 0, (LPARAM)&marginsRect);
}
void
ListWidget_TooltipRelayMouseMessage(ListWidgetTooltip *tooltip, unsigned int message, unsigned int vKeys, const POINT *cursor)
{
if (NULL != tooltip &&
NULL != tooltip->window &&
FALSE != tooltip->active)
{
MSG msg;
msg.hwnd = tooltip->owner;
msg.message = message;
msg.wParam = (WPARAM)vKeys;
msg.lParam = MAKELPARAM(cursor->x, cursor->y);
SendMessage(tooltip->window, TTM_RELAYEVENT, 0, (LPARAM)&msg);
}
}
void
ListWidget_TooltipHide(ListWidgetTooltip *tooltip)
{
if (NULL == tooltip ||
NULL == tooltip->window ||
FALSE == tooltip->active)
{
return;
}
tooltip->active = FALSE;
tooltip->part = ListWidgetItemPart_None;
SetRectEmpty(&tooltip->partRect);
SendMessage(tooltip->window, TTM_ACTIVATE, FALSE, 0);
}
BOOL
ListWidget_TooltipActivate(ListWidgetTooltip *tooltip, const RECT *rect)
{
TOOLINFO ti;
POINT origin;
if (NULL == tooltip ||
NULL == tooltip->window)
{
return FALSE;
}
ZeroMemory(&ti, sizeof(ti));
ti.cbSize = sizeof(ti);
ti.hwnd = tooltip->owner;
ti.uId = 0;
if (FALSE == SendMessage(tooltip->window, TTM_GETTOOLINFO, 0, (LPARAM)&ti))
return FALSE;
if (FALSE != tooltip->active)
{
tooltip->active = FALSE;
SendMessage(tooltip->window, TTM_ACTIVATE, FALSE, 0);
}
else
SendMessage(tooltip->window, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(TOOLTIP_DELAY_INITIAL, 0));
CopyRect(&ti.rect, rect);
if (FALSE != ListWidget_GetViewOrigin(tooltip->owner, &origin))
OffsetRect(&ti.rect, origin.x, origin.y);
ti.lParam = NULL;
ti.lpszText = LPSTR_TEXTCALLBACK;
SendMessage(tooltip->window, TTM_SETTOOLINFO, 0, (LPARAM)&ti);
if (FALSE == tooltip->active)
{
KillTimer(tooltip->window, 4);
SendMessage(tooltip->window, TTM_ACTIVATE, TRUE, 0);
tooltip->active = TRUE;
}
return TRUE;
}
BOOL
ListWidget_TooltipUpdate(ListWidgetTooltip *tooltip, ListWidgetItem *item,
ListWidgetItemPart part, const RECT *partRect)
{
BOOL tooltipValid;
if (NULL == tooltip)
return FALSE;
if(FALSE == tooltip->active && NULL == item)
return FALSE;
if (0 != (ListWidgetItemPart_Activity & part))
{
part &= ~ListWidgetItemPart_Activity;
part |= ListWidgetItemPart_Frame;
}
tooltipValid = (NULL != item && ListWidgetItemPart_None != part);
if (tooltip->item == item &&
tooltip->part == part &&
(FALSE != (FALSE != tooltipValid) ?
EqualRect(&tooltip->partRect, partRect) :
IsRectEmpty(&tooltip->partRect)))
{
return FALSE;
}
tooltip->item = item;
tooltip->part = part;
if (FALSE != tooltipValid)
CopyRect(&tooltip->partRect, partRect);
else
SetRectEmpty(&tooltip->partRect);
if (FALSE == tooltipValid)
{
ListWidget_TooltipHide(tooltip);
return FALSE;
}
return ListWidget_TooltipActivate(tooltip, &tooltip->partRect);
}
static BOOL
ListWidget_TooltipFormatTip(ListWidget *self, HWND hwnd,
const RECT *rect, wchar_t *buffer, size_t bufferMax)
{
WidgetStyle *style;
ListWidgetItem *item;
ListWidgetItemPart part;
ListWidgetItemMetric metrics;
RECT partRect;
POINT pt, origin;
if (NULL == self || NULL == rect)
return FALSE;
style = WIDGET_GET_STYLE(hwnd);
if (NULL == style)
return FALSE;
if (FALSE == ListWidget_GetItemMetrics(style, &metrics))
return FALSE;
if (FALSE == ListWidget_GetViewOrigin(hwnd, &origin))
{
origin.x = 0;
origin.y = 0;
}
pt.x = (rect->left + (rect->right - rect->left)/2) - origin.x;
pt.y = (rect->top + (rect->bottom - rect->top)/2) - origin.y;
item = ListWidget_GetItemFromPoint(self, pt);
if (NULL == item)
return FALSE;
part = ListWidgetItemPart_Frame |
ListWidgetItemPart_Command |
ListWidgetItemPart_Spacebar |
ListWidgetItemPart_Title;
part = ListWidget_GetItemPartFromPoint(self, item, &metrics, pt, part, &partRect);
switch(part)
{
case ListWidgetItemPart_Command:
CopyRect(&partRect, rect);
OffsetRect(&partRect, -origin.x - item->rect.left, -origin.y - item->rect.top);
return ListWidget_FormatItemCommandTip(self, item, &partRect, buffer, bufferMax);
case ListWidgetItemPart_Activity:
case ListWidgetItemPart_Frame:
return ListWidget_FormatItemTip(self, item, buffer, bufferMax);
case ListWidgetItemPart_Spacebar:
return ListWidget_FormatItemSpaceTip(self, item, buffer, bufferMax);
case ListWidgetItemPart_Title:
return ListWidget_FormatItemTitleTip(self, item, buffer, bufferMax);
}
return FALSE;
}
static void
ListWidget_TooltipGetDispInfo(ListWidget *self, ListWidgetTooltip *tooltip, NMTTDISPINFO *dispInfo)
{
TOOLINFO ti;
if (NULL == dispInfo)
return;
ZeroMemory(&ti, sizeof(ti));
ti.cbSize = sizeof(ti);
ti.hwnd = tooltip->owner;
ti.uId = dispInfo->hdr.idFrom;
String_Free(tooltip->buffer);
tooltip->buffer = NULL;
if (FALSE != SendMessage(dispInfo->hdr.hwndFrom, TTM_GETTOOLINFO, 0, (LPARAM)&ti))
{
wchar_t buffer[4096] = {0};
if (FALSE != ListWidget_TooltipFormatTip(self, tooltip->owner, &ti.rect, buffer, ARRAYSIZE(buffer)))
tooltip->buffer = String_Duplicate(buffer);
}
dispInfo->lpszText = tooltip->buffer;
dispInfo->szText[0] = L'\0';
dispInfo->hinst = NULL;
dispInfo->uFlags = TTF_DI_SETITEM;
}
BOOL
ListWidget_TooltipProcessNotification(ListWidget *self, ListWidgetTooltip *tooltip, NMHDR *pnmh, LRESULT *result)
{
if (NULL == tooltip ||
NULL == pnmh ||
pnmh->hwndFrom != tooltip->window)
{
return FALSE;
}
switch(pnmh->code)
{
case TTN_GETDISPINFO:
ListWidget_TooltipGetDispInfo(self, tooltip, (NMTTDISPINFO*)pnmh);
break;
case TTN_SHOW:
if (0 == (WS_VISIBLE & GetWindowStyle(tooltip->window)))
tooltip->showTime = GetTickCount();
if (FALSE != tooltip->active)
{
SendMessage(tooltip->window, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(TOOLTIP_DELAY_RESHOW, 0));
}
break;
}
return TRUE;
}
ListWidgetItem *
ListWidget_TooltipGetCurrent(ListWidgetTooltip *tooltip, ListWidgetItemPart *part, RECT *partRect)
{
if (NULL == tooltip ||
FALSE == tooltip->active ||
NULL == tooltip->item)
{
return NULL;
}
if (NULL != part)
*part = tooltip->part;
if (NULL != partRect)
CopyRect(partRect, &tooltip->partRect);
return tooltip->item;
}
BOOL
ListWidget_TooltipGetChanged(ListWidgetTooltip *tooltip, ListWidgetItem *item,
ListWidgetItemPart part, const RECT *partRect)
{
if (NULL == tooltip)
return FALSE;
if (tooltip->item != item)
return TRUE;
if (NULL == item)
return FALSE;
if (tooltip->part != part)
return TRUE;
if (FALSE == EqualRect(&tooltip->partRect, partRect))
return TRUE;
return FALSE;
}
BOOL
ListWidget_TooltipUpdateText(ListWidget *self, ListWidgetTooltip *tooltip, ListWidgetItem *item, TooltipUpdateReason reason)
{
TOOLINFO ti;
ListWidgetItemPart mask;
unsigned int windowStyle;
unsigned long showTimeout, currentTime;
if (NULL == self ||
NULL == tooltip ||
NULL == tooltip->active ||
NULL == tooltip->window)
{
return FALSE;
}
if (NULL != item && tooltip->item != item)
return FALSE;
windowStyle = GetWindowStyle(tooltip->window);
if (0 == (WS_VISIBLE & windowStyle))
return FALSE;
showTimeout = (unsigned long)SendMessage(tooltip->window, TTM_GETDELAYTIME, (WPARAM)TTDT_AUTOPOP, 0L);
currentTime = GetTickCount();
if (currentTime < tooltip->showTime)
return FALSE;
currentTime -= tooltip->showTime;
if (showTimeout > (currentTime + 10))
showTimeout -= currentTime;
else
return FALSE;
KillTimer(tooltip->window, 4);
switch(reason)
{
case Tooltip_DeviceTitleChanged:
case Tooltip_DeviceModelChanged:
case Tooltip_DeviceStatusChanged:
mask = ListWidgetItemPart_Frame |
ListWidgetItemPart_Activity |
ListWidgetItemPart_Title;
if (0 == (mask & tooltip->part))
return FALSE;
break;
case Tooltip_DeviceSpaceChanged:
mask = ListWidgetItemPart_Frame |
ListWidgetItemPart_Activity |
ListWidgetItemPart_Spacebar;
if (0 == (mask & tooltip->part))
return FALSE;
break;
/*case Tooltip_DeviceActivityChanged:
mask = ListWidgetItemPart_Frame |
ListWidgetItemPart_Activity;
if (0 == (mask & tooltip->part))
return FALSE;
break;*/
}
ZeroMemory(&ti, sizeof(ti));
ti.cbSize = sizeof(ti);
ti.hwnd = tooltip->owner;
ti.uId = 0;
ti.lpszText = LPSTR_TEXTCALLBACK;
tooltip->blockLocationChange = TRUE;
SendMessage(tooltip->window, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
tooltip->blockLocationChange = FALSE;
SetTimer(tooltip->window, 4, showTimeout, 0);
return TRUE;
}
static LRESULT WINAPI
ListWidgetTooltip_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ListWidgetTooltip *tooltip;
tooltip = (ListWidgetTooltip*)GetProp(hwnd, MAKEINTATOM(LISTWIDGETTOOLTIP_PROP));
if (NULL == tooltip ||
NULL == tooltip->originalProc)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
switch(uMsg)
{
case WM_DESTROY:
RemoveProp(hwnd, MAKEINTATOM(LISTWIDGETTOOLTIP_PROP));
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)tooltip->originalProc);
CallWindowProc(tooltip->originalProc, hwnd, uMsg, wParam, lParam);
tooltip->originalProc = NULL;
tooltip->window = NULL;
break;
case WM_WINDOWPOSCHANGING:
if (FALSE != tooltip->blockLocationChange)
{
WINDOWPOS *pwp;
pwp = (WINDOWPOS*)lParam;
if (NULL != pwp)
{
pwp->flags |= SWP_NOMOVE;
}
}
break;
}
return CallWindowProc(tooltip->originalProc, hwnd, uMsg, wParam, lParam);
}
|