#15390 closed defect (fixed)

wxTreeCtrl::GetItemFont() returns null font - setting it again increases font size

Reported by: daumling Owned by:
Priority: low Milestone:
Component: documentation Version: 2.9.4
Keywords: Cc:
Blocked By: Blocking:
Patch: no

Description

Create a vanilla wxTreeCtrl, populate it with a few items, and get the font - the font contents are null.

To set a list item text to italics, I got the font, set the italics attribute and called SetItemFont() again. It worked, but the size was wrong (larger), probably due to the font not having been initialized.

This was a 32 bit app on Windows 7/64.

Change History (4)

comment:1 Changed 15 months ago by vadz

  • Status changed from new to infoneeded_new

Could you please make a minimal patch against the treectrl sample showing the problem? Alternatively, please try to check under debugger what's going on in GetFont(). I really don't see how could it return an invalid font considering that it always falls back on the default class font.

comment:2 Changed 15 months ago by daumling

  • Status changed from infoneeded_new to new

I checked GetItemFont(). The item is not part of m_attrs, so the call returns wxNullFont. So I thiught I'd be clever, and set the background color of the item to black to force an entry of the item in the m_attrs map, but no joy. The font stored in the entry's wxTreeItemAttr class appears to be the default system font.

IMHO, the change to ItemGetFont() should be as follows:

1) If the m_attrs map already has a wxTreeItemAttr entry, fine.
2) Create a new wxFont using the handle returned by a SendMessage(getHWND(), WM_GETFONT, 0, 0).
3) Store that font into a new wxTreeItemAttr and store it into the m_attrs map.
4) Return that font.

The following patch works, but it is ugly and nowhere near production quality:

#include <wx/fontutil.h>

wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
{

wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );

wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
if (it != m_attrs.end())

return it->second->GetFont();

HFONT hFont = (HFONT) ::SendMessage(GetHWND(), WM_GETFONT, 0, 0);
LOGFONT lf;
::GetObject(hFont, sizeof(LOGFONT), &lf);
wxNativeFontInfo dummy(lf);
wxFont font(dummy, hFont);
wxTreeCtrl* self = const_cast<wxTreeCtrl*>(this);
self->m_hasAnyAttr = true;
wxTreeItemAttr *attr;
self->m_attrs[item.m_pItem] =
attr = new wxTreeItemAttr;
attr->SetFont(font);
return attr->GetFont();

}

Hope this helps.

comment:3 Changed 15 months ago by vadz

  • Component changed from wxMSW to documentation
  • Priority changed from normal to low

Sorry, I misread the bug title and thought you were using GetFont() and not GetItemFont(). The latter indeed returns wxNullFont if no specific font is set for the item and this is intentional, to allow distinguishing the case of no font being set specifically for the item from the case of the control font being explicitly set for the item. I'll update the documentation to make this clear.

Summary: if you need default control font, use GetFont(), not GetItemFont().

comment:4 Changed 15 months ago by VZ

  • Resolution set to fixed
  • Status changed from new to closed

(In [74629]) Document wxTreeCtrl::GetItemFont() behaviour for items without font.

Explain that this method returns wxNullFont if no font was set for the item.

Closes #15390.

Note: See TracTickets for help on using tickets.