summaryrefslogtreecommitdiff
path: root/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'widgets')
-rw-r--r--widgets/FormattedListboxTextItem.cpp165
-rw-r--r--widgets/FormattedListboxTextItem.h54
-rw-r--r--widgets/README1
3 files changed, 220 insertions, 0 deletions
diff --git a/widgets/FormattedListboxTextItem.cpp b/widgets/FormattedListboxTextItem.cpp
new file mode 100644
index 0000000..b9362cc
--- /dev/null
+++ b/widgets/FormattedListboxTextItem.cpp
@@ -0,0 +1,165 @@
+#include "FormattedListboxTextItem.h"
+
+namespace CEGUI
+{
+//----------------------------------------------------------------------------//
+FormattedListboxTextItem::FormattedListboxTextItem(const String& text,
+ const HorizontalTextFormatting format,
+ const uint item_id,
+ void* const item_data,
+ const bool disabled,
+ const bool auto_delete) :
+ // initialise base class
+ ListboxTextItem(text, item_id, item_data, disabled, auto_delete),
+ // initialise subclass fields
+ d_formatting(format),
+ d_formattedRenderedString(0)
+{
+}
+
+//----------------------------------------------------------------------------//
+FormattedListboxTextItem::~FormattedListboxTextItem()
+{
+ delete d_formattedRenderedString;
+}
+
+//----------------------------------------------------------------------------//
+HorizontalTextFormatting FormattedListboxTextItem::getFormatting() const
+{
+ return d_formatting;
+}
+
+//----------------------------------------------------------------------------//
+void FormattedListboxTextItem::setFormatting(const HorizontalTextFormatting fmt)
+{
+ if (fmt == d_formatting)
+ return;
+
+ d_formatting = fmt;
+ delete d_formattedRenderedString;
+ d_formattedRenderedString = 0;
+ d_formattingAreaSize = Size(0, 0);
+}
+
+//----------------------------------------------------------------------------//
+Size FormattedListboxTextItem::getPixelSize(void) const
+{
+ if (!d_owner)
+ return Size(0, 0);
+
+ // reparse text if we need to.
+ if (!d_renderedStringValid)
+ parseTextString();
+
+ // create formatter if needed
+ if (!d_formattedRenderedString)
+ setupStringFormatter();
+
+ // get size of render area from target window, to see if we need to reformat
+ const Size area_sz(static_cast<const Listbox*>(d_owner)->
+ getListRenderArea().getSize());
+ if (area_sz != d_formattingAreaSize)
+ {
+ d_formattedRenderedString->format(area_sz);
+ d_formattingAreaSize = area_sz;
+ }
+
+ return Size(d_formattedRenderedString->getHorizontalExtent(),
+ d_formattedRenderedString->getVerticalExtent());
+}
+
+//----------------------------------------------------------------------------//
+void FormattedListboxTextItem::draw(GeometryBuffer& buffer,
+ const Rect& targetRect,
+ float alpha, const Rect* clipper) const
+{
+ // reparse text if we need to.
+ if (!d_renderedStringValid)
+ parseTextString();
+
+ // create formatter if needed
+ if (!d_formattedRenderedString)
+ setupStringFormatter();
+
+ // get size of render area from target window, to see if we need to reformat
+ // NB: We do not use targetRect, since it may not represent the same area.
+ const Size area_sz(static_cast<const Listbox*>(d_owner)->
+ getListRenderArea().getSize());
+ if (area_sz != d_formattingAreaSize)
+ {
+ d_formattedRenderedString->format(area_sz);
+ d_formattingAreaSize = area_sz;
+ }
+
+ // draw selection imagery
+ if (d_selected && d_selectBrush != 0)
+ d_selectBrush->draw(buffer, targetRect, clipper,
+ getModulateAlphaColourRect(d_selectCols, alpha));
+
+ // factor the window alpha into our colours.
+ const ColourRect final_colours(
+ getModulateAlphaColourRect(ColourRect(0xFFFFFFFF), alpha));
+
+ // draw the formatted text
+ d_formattedRenderedString->draw(buffer, targetRect.getPosition(),
+ &final_colours, clipper);
+}
+
+//----------------------------------------------------------------------------//
+void FormattedListboxTextItem::setupStringFormatter() const
+{
+ // delete any existing formatter
+ delete d_formattedRenderedString;
+ d_formattedRenderedString = 0;
+
+ // create new formatter of whichever type...
+ switch(d_formatting)
+ {
+ case HTF_LEFT_ALIGNED:
+ d_formattedRenderedString =
+ new LeftAlignedRenderedString(d_renderedString);
+ break;
+
+ case HTF_RIGHT_ALIGNED:
+ d_formattedRenderedString =
+ new RightAlignedRenderedString(d_renderedString);
+ break;
+
+ case HTF_CENTRE_ALIGNED:
+ d_formattedRenderedString =
+ new CentredRenderedString(d_renderedString);
+ break;
+
+ case HTF_JUSTIFIED:
+ d_formattedRenderedString =
+ new JustifiedRenderedString(d_renderedString);
+ break;
+
+ case HTF_WORDWRAP_LEFT_ALIGNED:
+ d_formattedRenderedString =
+ new RenderedStringWordWrapper
+ <LeftAlignedRenderedString>(d_renderedString);
+ break;
+
+ case HTF_WORDWRAP_RIGHT_ALIGNED:
+ d_formattedRenderedString =
+ new RenderedStringWordWrapper
+ <RightAlignedRenderedString>(d_renderedString);
+ break;
+
+ case HTF_WORDWRAP_CENTRE_ALIGNED:
+ d_formattedRenderedString =
+ new RenderedStringWordWrapper
+ <CentredRenderedString>(d_renderedString);
+ break;
+
+ case HTF_WORDWRAP_JUSTIFIED:
+ d_formattedRenderedString =
+ new RenderedStringWordWrapper
+ <JustifiedRenderedString>(d_renderedString);
+ break;
+ }
+}
+
+//----------------------------------------------------------------------------//
+}
diff --git a/widgets/FormattedListboxTextItem.h b/widgets/FormattedListboxTextItem.h
new file mode 100644
index 0000000..efe5ff2
--- /dev/null
+++ b/widgets/FormattedListboxTextItem.h
@@ -0,0 +1,54 @@
+#ifndef FORMATTEDLISTBOXTEXTITEM_H
+#define FORMATTEDLISTBOXTEXTITEM_H
+
+// TODO: Remove temporary workaround for CEGUI + GCC 4.6
+#include <cstddef>
+
+#include "CEGUI.h"
+
+namespace CEGUI
+{
+//! A ListboxItem based class that can do horizontal text formatiing.
+class FormattedListboxTextItem : public ListboxTextItem
+{
+public:
+ //! Constructor
+ FormattedListboxTextItem(const String& text,
+ const HorizontalTextFormatting format = HTF_LEFT_ALIGNED,
+ const uint item_id = 0,
+ void* const item_data = 0,
+ const bool disabled = false,
+ const bool auto_delete = true);
+
+ //! Destructor.
+ ~FormattedListboxTextItem();
+
+ //! Return the current formatting set.
+ HorizontalTextFormatting getFormatting() const;
+ /*!
+ Set the formatting. You should call Listbox::handleUpdatedItemData
+ after setting the formatting in order to update the listbox. We do not
+ do it automatically since you may wish to batch changes to multiple
+ items and multiple calls to handleUpdatedItemData is wasteful.
+ */
+ void setFormatting(const HorizontalTextFormatting fmt);
+
+ // overriden functions.
+ Size getPixelSize(void) const;
+ void draw(GeometryBuffer& buffer, const Rect& targetRect,
+ float alpha, const Rect* clipper) const;
+
+protected:
+ //! Helper to create a FormattedRenderedString of an appropriate type.
+ void setupStringFormatter() const;
+ //! Current formatting set
+ HorizontalTextFormatting d_formatting;
+ //! Class that renders RenderedString with some formatting.
+ mutable FormattedRenderedString* d_formattedRenderedString;
+ //! Tracks target area for rendering so we can reformat when needed
+ mutable Size d_formattingAreaSize;
+};
+
+}
+
+#endif
diff --git a/widgets/README b/widgets/README
new file mode 100644
index 0000000..3682ee3
--- /dev/null
+++ b/widgets/README
@@ -0,0 +1 @@
+FormattedListboxTextItem source: http://www.cegui.org.uk/phpBB2/viewtopic.php?f=10&t=4322