wxOwnerDrawnComboBox-inherited class 不可聚焦
wxOwnerDrawnComboBox-inherited class is not focusable
我实现了一个自己的组合框,用作字体选择器。为了直接在组合框中显示所选字体的外观,我创建了一个新的 class FontStyleComboBox,它继承自 wxOwnerDrawnComboBox。在显示完整的 class 之后,仅缺少字体管理部分:
class FontStyleComboBox : public wxOwnerDrawnComboBox
{
private:
std::vector<wxFont> m_fontList;
public:
virtual void OnDrawItem(wxDC& dc,const wxRect& rect,int item,int flags) const
{
if (item == wxNOT_FOUND) return;
wxCoord w1,h1,w2,h2;
dc.GetTextExtent(GetString(item),&w1,&h1);
dc.DrawText(GetString(item),
rect.x + 3,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
dc.SetFont(m_fontList[item]);
dc.GetTextExtent(GetString(item),&w2,&h2);
if (w2<250)
{
if (rect.x+10+w1>135)
dc.DrawText(_T("AaBbCcDd 1234"),
rect.x+10+w1,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
else
dc.DrawText(_T("AaBbCcDd 1234"),
135,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
}
}
virtual void OnDrawBackground(wxDC& dc, const wxRect& rect,int item, int flags) const
{
// If item is selected or even, or we are painting the
// combo control itself, use the default rendering.
if ((flags & (wxODCB_PAINTING_CONTROL | wxODCB_PAINTING_SELECTED)) ||
(item & 1) == 0)
{
wxOwnerDrawnComboBox::OnDrawBackground(dc, rect, item, flags);
return;
}
// Otherwise, draw every other background with different colour.
wxColour bgCol(245, 245, 255);
dc.SetBrush(wxBrush(bgCol));
dc.SetPen(wxPen(bgCol));
dc.DrawRectangle(rect);
}
virtual wxCoord OnMeasureItem(size_t item) const
{
return 20;
}
virtual wxCoord OnMeasureItemWidth(size_t item) const
{
return 400;
}
};
不幸的是,此字体组合框的行为与普通组合框不同:
- 在 GUI 中跳转元素时,一旦 FontStyleComboBox 应获得焦点,它就不会突出显示
- 在应该聚焦的地方,当按下 up/down 箭头时,它不会滑过组合框的元素,而是将焦点转移到 previous/next GUI 元素(右边有人会打 shift-tab/tab)
所以...知道这里可能遗漏了什么吗?我是否必须在此 class 中添加一些 focus/focus-lost 处理?
这发生在 wxWidgets 3.1 / Windows。
已解决:这是 Windows 的 3.1 版问题。对于 3.0/Linux 版,它从未发生过,并且从 3.1.4 版开始,它也已针对 Windows 进行了修复。
我实现了一个自己的组合框,用作字体选择器。为了直接在组合框中显示所选字体的外观,我创建了一个新的 class FontStyleComboBox,它继承自 wxOwnerDrawnComboBox。在显示完整的 class 之后,仅缺少字体管理部分:
class FontStyleComboBox : public wxOwnerDrawnComboBox
{
private:
std::vector<wxFont> m_fontList;
public:
virtual void OnDrawItem(wxDC& dc,const wxRect& rect,int item,int flags) const
{
if (item == wxNOT_FOUND) return;
wxCoord w1,h1,w2,h2;
dc.GetTextExtent(GetString(item),&w1,&h1);
dc.DrawText(GetString(item),
rect.x + 3,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
dc.SetFont(m_fontList[item]);
dc.GetTextExtent(GetString(item),&w2,&h2);
if (w2<250)
{
if (rect.x+10+w1>135)
dc.DrawText(_T("AaBbCcDd 1234"),
rect.x+10+w1,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
else
dc.DrawText(_T("AaBbCcDd 1234"),
135,
(rect.y + 0) + (rect.height / 2) - (dc.GetCharHeight() / 2)
);
}
}
virtual void OnDrawBackground(wxDC& dc, const wxRect& rect,int item, int flags) const
{
// If item is selected or even, or we are painting the
// combo control itself, use the default rendering.
if ((flags & (wxODCB_PAINTING_CONTROL | wxODCB_PAINTING_SELECTED)) ||
(item & 1) == 0)
{
wxOwnerDrawnComboBox::OnDrawBackground(dc, rect, item, flags);
return;
}
// Otherwise, draw every other background with different colour.
wxColour bgCol(245, 245, 255);
dc.SetBrush(wxBrush(bgCol));
dc.SetPen(wxPen(bgCol));
dc.DrawRectangle(rect);
}
virtual wxCoord OnMeasureItem(size_t item) const
{
return 20;
}
virtual wxCoord OnMeasureItemWidth(size_t item) const
{
return 400;
}
};
不幸的是,此字体组合框的行为与普通组合框不同:
- 在 GUI 中跳转元素时,一旦 FontStyleComboBox 应获得焦点,它就不会突出显示
- 在应该聚焦的地方,当按下 up/down 箭头时,它不会滑过组合框的元素,而是将焦点转移到 previous/next GUI 元素(右边有人会打 shift-tab/tab)
所以...知道这里可能遗漏了什么吗?我是否必须在此 class 中添加一些 focus/focus-lost 处理?
这发生在 wxWidgets 3.1 / Windows。
已解决:这是 Windows 的 3.1 版问题。对于 3.0/Linux 版,它从未发生过,并且从 3.1.4 版开始,它也已针对 Windows 进行了修复。