Newlines in tooltips

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Newlines in tooltips

Post by acsMike » Tue Jun 14, 2005 12:04 pm

Hi all,
I am trying to wordwrap a longish tooltip under MSW. To do that I though you would insert a newline char like this:

Code: Select all

wxT("Firstline.\nSecondline.")
But I just get one long line with a "square" where the newline should be.
Is this a Windows problem? What to do? :?
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Re: Newlines in tooltips

Post by ABX » Tue Jun 14, 2005 12:32 pm

acsMike wrote:Hi all,
I am trying to wordwrap a longish tooltip under MSW. To do that I though you would insert a newline char like this:

Code: Select all

wxT("Firstline.\nSecondline.")
But I just get one long line with a "square" where the newline should be.
Is this a Windows problem? What to do? :?
I do not know. I definietly see code for handling this in src/msw/tooltip.cpp and seems it fits MS description from http://msdn.microsoft.com/library/defau ... oltips.asp . Dumb question but does replacing '\n' with '\r\n' to be correct with MS description changes this?

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Post by acsMike » Tue Jun 14, 2005 1:01 pm

Using "\r\n" gives no linebreak, but instead 2 "squares"! - As if the 2 chars are not recognised!

Thanks though!
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Tue Jun 14, 2005 5:55 pm

acsMike wrote:Using "\r\n" gives no linebreak, but instead 2 "squares"! - As if the 2 chars are not recognised!
I think I found it. Your build either does not have correct SDK available or use older comctl32.dll. In such case tooltip.cpp went into replacement new lines with spaces but this was broken because due to optimization in wxStringBase::CopyBeforeWrite() the replacement occured in different text than the one available for tooltip control. It is now fixed in CVS head for me.

Here is change required in tooltip.cpp:
http://cvs.wxwidgets.org/viewcvs.cgi/wx ... 42&r2=1.43

Following change in widgets sample allows testing it:
http://cvs.wxwidgets.org/viewcvs.cgi/wx ... 33&r2=1.34

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Post by acsMike » Tue Jun 14, 2005 8:35 pm

Wow, thanks.
I will try it out when I get back in a few days.
A note though; I am trying this on XP so I guess I have a good comctl32.dll. I tried on wx 2.6.1 (i.e file version 1.42 which you now patched.)

The tooltip in question is dynamically re-generated; i.e from time to time I replace the tooltip text. Do you think it matters? I'll let you know in a few days :-)
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Wed Jun 15, 2005 6:43 am

acsMike wrote:A note though; I am trying this on XP so I guess I have a good comctl32.dll. I tried on wx 2.6.1 (i.e file version 1.42 which you now patched.)
I do not know what's exactly with your case (could it be own/outdated VC6 headers?). But the change I provided surely fixes something ;-) because I was able to duplicate displaying of rectangles with latest DMC compiler. Among all other compilers (DMC, OW,MinGW,Borland,free VC 7.1) only DMC didn't support multiline tooltips.
acsMike wrote:The tooltip in question is dynamically re-generated; i.e from time to time I replace the tooltip text. Do you think it matters?
That should not be a problem but it definietly needs checking. With current version of widgets sample you can test dynamic changing of tooltip text.
acsMike wrote:I'll let you know in a few days :-)
Thanks, ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Post by acsMike » Mon Jun 20, 2005 2:51 pm

Hi again, I have now tested version 1.43 - but I still have this problem. I have run on various systems, including Xp, so it's probably not a comctl32.dll problem.

I still get squares instead of newlines or spaces so I don't think the new code is acctually run.

I am at a loss right now... :? Hmmm
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Post by acsMike » Mon Jun 20, 2005 3:05 pm

Found a workaround; and possibly a hint to the nature of the problem:

Instead of dynamically changing the tooltip like this:

Code: Select all

SetToolTip(wxT("New text"));
I do the following:

Code: Select all

SetToolTip((wxToolTip *)NULL);
SetToolTip(wxT("New text"));
I assume, if the very first tooltip did not contain a newline char, an updated tooltip which does contain newlines will not update properly.
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Mon Jun 20, 2005 3:05 pm

acsMike wrote:Hi again, I have now tested version 1.43 - but I still have this problem. I have run on various systems, including Xp, so it's probably not a comctl32.dll problem.
Please modify samples/minimal/minimal.cpp sample so it still presents your problem and then make this new minimal.cpp sample somehow available for me. (best with screenshots)
acsMike wrote:I don't think the new code is acctually run.
The new code is definietly run because with DMC I got squares before my change and space instead of square after change.

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

acsMike
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Feb 11, 2005 9:05 am
Location: Stockholm, Sweden

Post by acsMike » Mon Jun 20, 2005 3:27 pm

Could you please give me your email? Can't seem to be able to post attachments on the board. My email is [email protected]
/Michael Ljunggren, developer
Peavey Electronics

Windows: 2.6.3, 2.8.9 (VS2005) ** Mac: 2.6.3 (XCode 3) ** WinCE: 2.8.9 (VS2005), ** GTK: 2.8.9 (Kdevelop)

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Mon Jun 20, 2005 3:38 pm

acsMike wrote:Could you please give me your email? Can't seem to be able to post attachments on the board. My email is [email protected]
Please use wxWidgets bug tracker (which I should suggest at the very beginning). Assign your entry to me ("abxabx") and I will find it. Thanks!

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Tue Aug 23, 2005 9:41 pm

has any investigation be done about this ?
in wx-cvs there is no change in /src/msw/tooltip.cpp , it's still version 1.43
// RCS-ID: $Id: tooltip.cpp,v 1.43 2005/06/14 17:43:45 ABX Exp $

i have similar problems as described by ascMike,
from XRC resources loaded tooltips, which are shown perfectly as multiline tooltips in an ANSI build of the wxApp,
shows a small rectangle instead of the newline when building a unicode version.

btw. i'm running Windows XP , wxApp::GetComCtl32Version() results 600
-tiwag

Vaulter
Earned a small fee
Earned a small fee
Posts: 19
Joined: Thu Jun 30, 2005 7:49 am
Location: Russia
Contact:

Post by Vaulter » Fri Sep 09, 2005 1:41 pm

patch from 1.42 to 1.43 doesn't solve this problem
i suggest this code of wxToolTip::SetTip

Code: Select all

void wxToolTip::SetTip(const wxString& tip)
{
    m_text = tip;
    if ( m_window )
    {
        // update the tip text shown by the control
        wxToolInfo ti(GetHwndOf(m_window));
        ti.lpszText = (wxChar *)m_text.c_str();
        if ( m_text.Find(_T('\n')) != wxNOT_FOUND )
        {//multiline
#ifdef TTM_SETMAXTIPWIDTH
            if ( wxApp::GetComCtl32Version() >= 470 )
            {
				int pos1 = 0,pos2 = m_text.Find(_T('\n')), max = pos2-pos1;
				while(pos2 != wxNOT_FOUND)
				{
					if((pos2-pos1) > max)max = pos2-pos1;
					pos1 = pos2; pos2 = m_text.find(_T('\n'),pos2 + 1);
				}
pos2 = m_text.Length(); if((pos2-pos1) > max)max = pos2-pos1;
                // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
                // extent of its first line as max value
                HFONT hfont = (HFONT)SendTooltipMessage(GetToolTipCtrl(),
                                                        WM_GETFONT,
                                                        0, 0);
                if ( !hfont )
                {
                    hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
                    if ( !hfont )
                    {
                        wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
                    }
                }

                MemoryHDC hdc;
                if ( !hdc )
                {
                    wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
                }

                if ( !SelectObject(hdc, hfont) )
                {
                    wxLogLastError(wxT("SelectObject(hfont)"));
                }

                SIZE sz;
                if ( !::GetTextExtentPoint32(hdc, m_text, max, &sz) )
                {
                    wxLogLastError(wxT("GetTextExtentPoint32"));
                }

                SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
                                   0, (void *)sz.cx);
            }
#endif // comctl32.dll >= 4.70

            // replace the '\n's with spaces because otherwise they appear as
            // unprintable characters in the tooltip string
            m_text.Replace(_T("\n"), _T(" "));

		}


        (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
    }
}
Last edited by Vaulter on Mon Sep 12, 2005 11:08 am, edited 1 time in total.

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Fri Sep 09, 2005 1:56 pm

Vaulter wrote:patch from 1.42 to 1.43 doesn't solve this problem
i suggest this code of wxToolTip::SetTip
I see the change in the code but I do not want to apply it blindly so I would like to ask for some more from you. Could you please write some explanation why do you want once again measure the font in that place? Moreover could you modernize your patch so it could not duplicate code from other place of tooltip.cpp?

As suggested earlier, feel free to post your improved together with explanation directly into patch tracker and assign it to me (abxabx). Thanks in advance!

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

Vaulter
Earned a small fee
Earned a small fee
Posts: 19
Joined: Thu Jun 30, 2005 7:49 am
Location: Russia
Contact:

Post by Vaulter » Mon Sep 12, 2005 6:40 am

ABX wrote: I see the change in the code but I do not want to apply it blindly so I would like to ask for some more from you. Could you please write some explanation why do you want once again measure the font in that place? Moreover could you modernize your patch so it could not duplicate code from other place of tooltip.cpp?

As suggested earlier, feel free to post your improved together with explanation directly into patch tracker and assign it to me (abxabx). Thanks in advance!

ABX
that's right! i'll try to explain this decision.
1. i have wxTreeCtrl, and i was needed multiline tootip (screenshot). But, when wxTreeCtrl constructed, it call wxToolTip.Add() with "" text, so, SETMAXTIPWIDTH do not sended to tooltip,
2. for each item i need different tooltips, with different maxwidths, so i think that in SetTip it would be naturally to send SETMAXTIPWIDTH.
3. and max width better to make by string with max length:

Code: Select all

int pos1 = 0,pos2 = m_text.Find(_T('\n')), max = pos2-pos1;
while(pos2 != wxNOT_FOUND)
{
      if((pos2-pos1) > max)max = pos2-pos1;
      pos1 = pos2; pos2 = m_text.find(_T('\n'),pos2 + 1);
}
pos2 = m_text.Length(); if((pos2-pos1) > max)max = pos2-pos1;
4. duplicate code is solve with carrying-out code with SETMAXTIPWIDTH (SetTipWidthFor(wxString ToolTip)) to single function and it could be called from Add and SetTip functions both.

ps: excuse me, i dont found bug tracker. :(

Post Reply