Ok. Here is a rough (but working) first draft of a ListboxTextItem based class that does word-wrapping.
This is not 100% perfect, but will be useful in many cases. There should probably be checks in place in case the looknfeel is not assigned properly, and also a check to make sure that d_owner really is a Listbox. This solution could be easily extended to allow for a configurable formatting, but anyway, you can base your own code on this
The class declaration is like so:
Code: Select all
namespace CEGUI
{
/*!
\brief
ListboxTextItem based class that renders item text with word-wrapping.
*/
class ListboxWordWrapItem : public ListboxTextItem
{
public:
ListboxWordWrapItem(const String& text,
uint item_id = 0,
void* item_data = 0,
bool disabled = false,
bool auto_delete = true);
// override the members related to drawing and layout
Size getPixelSize(void) const;
void draw(RenderCache& cache, const Rect& targetRect, float zBase,
float alpha, const Rect* clipper) const;
};
}
And the member definitions are like so:
Code: Select all
namespace CEGUI
{
//----------------------------------------------------------------------------//
ListboxWordWrapItem::ListboxWordWrapItem(const String& text, uint item_id,
void* item_data, bool disabled,
bool auto_delete) :
ListboxTextItem(text, item_id, item_data, disabled, auto_delete)
{
}
//----------------------------------------------------------------------------//
Size ListboxWordWrapItem::getPixelSize(void) const
{
Size sz(0,0);
Font* fnt = getFont();
// if no owner window or no font, return a zero size
if (!d_owner || !fnt)
return sz;
// get the looknfeel assigned to the listbox owner window
const WidgetLookFeel& wlf =
WidgetLookManager::getSingleton().getWidgetLook(d_owner->getLookNFeel());
// get the area for the named area "ItemRenderingArea"
Rect wnd_area =
wlf.getNamedArea("ItemRenderingArea").getArea().getPixelRect(*d_owner);
size_t lines = fnt->getFormattedLineCount(d_itemText,
wnd_area,
WordWrapLeftAligned);
sz.d_height = PixelAligned(fnt->getLineSpacing()) * lines;
sz.d_width = PixelAligned(fnt->getFormattedTextExtent(d_itemText,
wnd_area,
WordWrapLeftAligned));
return sz;
}
//----------------------------------------------------------------------------//
void ListboxWordWrapItem::draw(RenderCache& cache,const Rect& targetRect,
float zBase, float alpha,
const Rect* clipper) const
{
// render selection brush into item area if item is selected
if (d_selected && d_selectBrush != 0)
cache.cacheImage(*d_selectBrush, targetRect, zBase,
getModulateAlphaColourRect(d_selectCols, alpha),
clipper);
Font* font = getFont();
// render text with word wrapped formatting.
if (font)
{
Rect finalPos(targetRect);
finalPos.d_top +=
PixelAligned((font->getLineSpacing() - font->getFontHeight()) / 2);
cache.cacheText(d_itemText, font, WordWrapLeftAligned, finalPos, zBase,
getModulateAlphaColourRect(d_textCols, alpha), clipper);
}
}
//----------------------------------------------------------------------------//
}
HTH
CE