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

1 view
Skip to first unread message

wxTrac

unread,
Aug 6, 2013, 12:39:19 AM8/6/13
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/15390>

#15390: wxTreeCtrl::GetItemFont() returns null font - setting it again increases
font size
----------------------+-----------------------------------------------------
Reporter: daumling | Owner:
Type: defect | Status: new
Priority: normal | Milestone:
Component: wxMSW | Version: 2.9.4
Keywords: | Blockedby:
Patch: 0 | Blocking:
----------------------+-----------------------------------------------------
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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/15390>

wxTrac

unread,
Aug 6, 2013, 5:50:48 AM8/6/13
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:1>

#15390: wxTreeCtrl::GetItemFont() returns null font - setting it again increases
font size
----------------------+-----------------------------------------------------
Reporter: daumling | Owner:
Type: defect | Status: infoneeded_new
Priority: normal | Milestone:
Component: wxMSW | Version: 2.9.4
Keywords: | Blockedby:
Patch: 0 | Blocking:
----------------------+-----------------------------------------------------
Changes (by vadz):

* status: new => infoneeded_new


Comment:

Could you please make a [HowToSubmitPatches 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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:1>

wxTrac

unread,
Aug 6, 2013, 11:31:26 AM8/6/13
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:2>

#15390: wxTreeCtrl::GetItemFont() returns null font - setting it again increases
font size
----------------------+-----------------------------------------------------
Reporter: daumling | Owner:
Type: defect | Status: new
Priority: normal | Milestone:
Component: wxMSW | Version: 2.9.4
Keywords: | Blockedby:
Patch: 0 | Blocking:
----------------------+-----------------------------------------------------
Changes (by daumling):

* status: infoneeded_new => new


Comment:

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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:2>

wxTrac

unread,
Aug 6, 2013, 12:07:28 PM8/6/13
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:3>

#15390: wxTreeCtrl::GetItemFont() returns null font - setting it again increases
font size
---------------------------+------------------------------------------------
Reporter: daumling | Owner:
Type: defect | Status: new
Priority: low | Milestone:
Component: documentation | Version: 2.9.4
Keywords: | Blockedby:
Patch: 0 | Blocking:
---------------------------+------------------------------------------------
Changes (by vadz):

* priority: normal => low
* component: wxMSW => documentation


Comment:

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()`.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:3>

wxTrac

unread,
Aug 6, 2013, 12:59:51 PM8/6/13
to wx-...@googlegroups.com
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:4>

#15390: wxTreeCtrl::GetItemFont() returns null font - setting it again increases
font size
----------------------------+-----------------------------------------------
Reporter: daumling | Owner:
Type: defect | Status: closed
Priority: low | Milestone:
Component: documentation | Version: 2.9.4
Resolution: fixed | Keywords:
Blockedby: | Patch: 0
Blocking: |
----------------------------+-----------------------------------------------
Changes (by VZ):

* status: new => closed
* resolution: => fixed


Comment:

(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.


--
Ticket URL: <http://trac.wxwidgets.org/ticket/15390#comment:4>
Reply all
Reply to author
Forward
0 new messages