Skip to content

Fit Table Tooltip in a single monitor #62 #2241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7158,82 +7158,77 @@ LRESULT wmNotifyToolTip (NMHDR hdr, long wParam, long lParam) {
case OS.TTN_SHOW: {
LRESULT result = super.wmNotify (hdr, wParam, lParam);
if (result != null) return result;
if (hdr.code != OS.TTN_SHOW) tipRequested = true;
long code = callWindowProc (handle, OS.WM_NOTIFY, wParam, lParam);
if (hdr.code != OS.TTN_SHOW) tipRequested = false;
Comment on lines -7161 to -7163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can this code be removed? It will also result in the code returned by this method to usually be null (in case we are not in the customToolTip case with TTN_SHOW.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was made corresponding to this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=178044
Commit id: ec9593c
This was made for rollovers in 2007 but seems to have been fixed for the newer versions of windows. With the block present, the code goes in an infinite loop at 7162 and gets infinite TTN_GETDISPINFO messages, when the tooltip is by default over 2 monitors.
Thus, this could be removed now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current windows works fine with rollover tooltips withut this block as well, however this block makes it worse when the tooltip is in the middle of 2 screens. You can try running the code with this block present and try to position your tooltip in the middle of 2 monitors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit that I do not fully understand the assessment. The code was not introduced for the mentioned bug and in the mentioned commit. The commit only moved that code to a different place, such that it is also executed for the TTN_SHOW event. The commit that originally introduced the code is from 2005: dd05f31. So the question remains why the code is now completely unnecessary (not only for TTN_SHOW but only for the TTN_GETDISPINFO event).

Just wondering: is the reaction to the TTN_GETDISPINFO still necessary at all? I tested with the ControlExample and there it does not make any difference whether I keep the switch-case for TTN_GETDISPINFO or whether I remove it (even though those events are actually fired). But since I don't know what this is necessary for, I cannot assess if there are remaining cases (maybe with very specific configurations) where it is needed.

In general, I currently still lack understanding of what exactly happens here. I can confirm that the change works as expected in all cases I have tested, but likely I don't know about all the use cases this complex code has been written for. So I am bit concerned that we break something when just removing some code. On the other hand, the code obviously leads to an issue (namely an inifite execution loop), which we definitely need to resolve. So it might lead to the trade-off that we need to accept that we might miss some use case here that we do not understand but therefor resolve a known bug.

Thus, my proposal:

  • Please check again what this code is used for (as, like I said above, the existing assessment is not comprehensible for me)
  • Have the change reviewed and tested by @fedejeanne, such that he can hopefully merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no explanation in the commit dd05f31, however it relates to the bug report mentioned I suppose. (https://bugs.eclipse.org/bugs/show_bug.cgi?id=82739)

There's a snippet in the report and I tried that out and found no issues. I believe the block of code:

tipRequested = true;Add commentMore actions
int code = callWindowProc (handle, OS.WM_NOTIFY, wParam, lParam);
tipRequested = false;

Doesn't have any visual effect anymore.

Anyway it's hard to assess what it was introduced for but I gathered that the tooltip works similar to how it is implemented for Tree. Without having much changes in the original implementation, I tried to bring it as close as possible to Tree. However, I assessed the bug report https://bugs.eclipse.org/bugs/show_bug.cgi?id=82739 and the other bug report which was its duplicate (https://bugs.eclipse.org/bugs/show_bug.cgi?id=83703) and it doesn't seem to hamper the table tooltips in externalize stings view, the custom tooltip on the table in the control which comes using (ctrl+3) and also the snippets. These fixes were done for XP and Vista as it seems but I have tested them with the current windows 11 and my PR doesn't change any behaviour at all for them.

if (toolTipText != null) break;
if (isCustomToolTip ()) {
LVHITTESTINFO pinfo = new LVHITTESTINFO ();
int pos = OS.GetMessagePos ();
POINT pt = new POINT();
OS.POINTSTOPOINT (pt, pos);
OS.ScreenToClient (handle, pt);
pinfo.x = pt.x;
pinfo.y = pt.y;
/*
* Bug in Windows. When LVM_SUBITEMHITTEST is used to hittest
* a point that is above the table, instead of returning -1 to
* indicate that the hittest failed, a negative index is returned.
* The fix is to consider any value that is negative a failure.
*/
if (OS.SendMessage (handle, OS.LVM_SUBITEMHITTEST, 0, pinfo) >= 0) {
TableItem item = _getItem (pinfo.iItem);
long hDC = OS.GetDC (handle);
long oldFont = 0, newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
long hFont = item.fontHandle (pinfo.iSubItem);
if (hFont != -1) hFont = OS.SelectObject (hDC, hFont);
Event event = sendMeasureItemEvent (item, pinfo.iItem, pinfo.iSubItem, hDC);
if (!isDisposed () && !item.isDisposed ()) {
RECT itemRect = new RECT ();
Rectangle boundsInPixels = DPIUtil.scaleUp(event.getBounds(), getZoom());
OS.SetRect (itemRect, boundsInPixels.x, boundsInPixels.y, boundsInPixels.x + boundsInPixels.width, boundsInPixels.y + boundsInPixels.height);
if (hdr.code == OS.TTN_SHOW) {
RECT toolRect = toolTipRect (itemRect);
OS.MapWindowPoints (handle, 0, toolRect, 2);
long hwndToolTip = OS.SendMessage (handle, OS.LVM_GETTOOLTIPS, 0, 0);
int flags = OS.SWP_NOACTIVATE | OS.SWP_NOZORDER;
int width = toolRect.right - toolRect.left, height = toolRect.bottom - toolRect.top;
OS.SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);
} else {
NMTTDISPINFO lpnmtdi = new NMTTDISPINFO ();
OS.MoveMemory (lpnmtdi, lParam, NMTTDISPINFO.sizeof);
if (lpnmtdi.lpszText != 0) {
OS.MoveMemory (lpnmtdi.lpszText, new char [1], 2);
OS.MoveMemory (lParam, lpnmtdi, NMTTDISPINFO.sizeof);
}
RECT cellRect = item.getBounds (pinfo.iItem, pinfo.iSubItem, true, true, true, true, hDC);
RECT clientRect = new RECT ();
OS.GetClientRect (handle, clientRect);
if (itemRect.right > cellRect.right || itemRect.right > clientRect.right) {
//TEMPORARY CODE
String string = " ";
// String string = null;
// if (pinfo.iSubItem == 0) {
// string = item.text;
// } else {
// String [] strings = item.strings;
// if (strings != null) string = strings [pinfo.iSubItem];
// }
if (string != null) {
Shell shell = getShell ();
char [] chars = new char [string.length () + 1];
string.getChars (0, string.length (), chars, 0);
shell.setToolTipText (lpnmtdi, chars);
OS.MoveMemory (lParam, lpnmtdi, NMTTDISPINFO.sizeof);
}
}
}
}
if (hFont != -1) hFont = OS.SelectObject (hDC, hFont);
if (newFont != 0) OS.SelectObject (hDC, oldFont);
OS.ReleaseDC (handle, hDC);
result = positionTooltip(hdr, lParam);
return result;
}
}
return null;
}

private LRESULT positionTooltip(NMHDR hdr, long lParam) {
LRESULT result = null;
LVHITTESTINFO pinfo = new LVHITTESTINFO ();
int pos = OS.GetMessagePos ();
POINT pt = new POINT();
OS.POINTSTOPOINT (pt, pos);
OS.ScreenToClient (handle, pt);
pinfo.x = pt.x;
pinfo.y = pt.y;
/*
* Bug in Windows. When LVM_SUBITEMHITTEST is used to hittest
* a point that is above the table, instead of returning -1 to
* indicate that the hittest failed, a negative index is returned.
* The fix is to consider any value that is negative a failure.
*/
if (OS.SendMessage (handle, OS.LVM_SUBITEMHITTEST, 0, pinfo) >= 0) {
TableItem item = _getItem (pinfo.iItem);
long hDC = OS.GetDC (handle);
long oldFont = 0, newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
long hFont = item.fontHandle (pinfo.iSubItem);
if (hFont != -1) hFont = OS.SelectObject (hDC, hFont);
Event event = sendMeasureItemEvent (item, pinfo.iItem, pinfo.iSubItem, hDC);
if (!isDisposed () && !item.isDisposed ()) {
RECT itemRect = new RECT ();
Rectangle boundsInPixels = DPIUtil.scaleUp(event.getBounds(), getZoom());
OS.SetRect (itemRect, boundsInPixels.x, boundsInPixels.y, boundsInPixels.x + boundsInPixels.width, boundsInPixels.y + boundsInPixels.height);
if (hdr.code == OS.TTN_SHOW) {
RECT toolRect = isCustomToolTip() ? toolTipRect (itemRect) : itemRect;
OS.MapWindowPoints (handle, 0, toolRect, 2);
long hwndToolTip = OS.SendMessage (handle, OS.LVM_GETTOOLTIPS, 0, 0);
int flags = OS.SWP_NOACTIVATE | OS.SWP_NOZORDER;
Rectangle adjustedTooltipBounds = getDisplay().fitRectangleBoundsIntoMonitorWithCursor(toolRect);
OS.SetWindowPos(hwndToolTip, 0, adjustedTooltipBounds.x, adjustedTooltipBounds.y,
adjustedTooltipBounds.width, adjustedTooltipBounds.height, flags);
result = LRESULT.ONE;
} else {
NMTTDISPINFO lpnmtdi = new NMTTDISPINFO ();
OS.MoveMemory (lpnmtdi, lParam, NMTTDISPINFO.sizeof);
if (lpnmtdi.lpszText != 0) {
OS.MoveMemory (lpnmtdi.lpszText, new char [1], 2);
OS.MoveMemory (lParam, lpnmtdi, NMTTDISPINFO.sizeof);
}
RECT cellRect = item.getBounds (pinfo.iItem, pinfo.iSubItem, true, true, true, true, hDC);
RECT clientRect = new RECT ();
OS.GetClientRect (handle, clientRect);
if (itemRect.right > cellRect.right || itemRect.right > clientRect.right) {
//TEMPORARY CODE
String string = " ";
Shell shell = getShell ();
char [] chars = new char [string.length () + 1];
string.getChars (0, string.length (), chars, 0);
shell.setToolTipText (lpnmtdi, chars);
OS.MoveMemory (lParam, lpnmtdi, NMTTDISPINFO.sizeof);
result = LRESULT.ONE;
}
}
return new LRESULT (code);
}
if (hFont != -1) hFont = OS.SelectObject (hDC, hFont);
if (newFont != 0) OS.SelectObject (hDC, oldFont);
OS.ReleaseDC (handle, hDC);
}
return null;
return result;
}

LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) {
Expand Down
Loading