Show font name and text preview on each FontItem

This commit is contained in:
David Capello 2015-10-16 18:52:52 -03:00
parent 2a09990b16
commit ec5a6fc8b3

View File

@ -47,7 +47,8 @@ class FontItem : public ListItem {
public: public:
FontItem(const std::string& fn) FontItem(const std::string& fn)
: ListItem(base::get_file_title(fn)) : ListItem(base::get_file_title(fn))
, m_filename(fn) { , m_filename(fn)
, m_loaded(false) {
} }
const std::string& filename() const { const std::string& filename() const {
@ -56,11 +57,9 @@ public:
private: private:
void onPaint(PaintEvent& ev) override { void onPaint(PaintEvent& ev) override {
if (!m_image) {
ListItem::onPaint(ev); ListItem::onPaint(ev);
}
else { if (m_image) {
app::skin::SkinTheme* theme = app::skin::SkinTheme::instance();
Graphics* g = ev.getGraphics(); Graphics* g = ev.getGraphics();
she::Surface* sur = she::instance()->createRgbaSurface(m_image->width(), she::Surface* sur = she::instance()->createRgbaSurface(m_image->width(),
m_image->height()); m_image->height());
@ -69,23 +68,19 @@ private:
m_image.get(), nullptr, sur, m_image.get(), nullptr, sur,
0, 0, 0, 0, m_image->width(), m_image->height()); 0, 0, 0, 0, m_image->width(), m_image->height());
gfx::Color bg; g->drawRgbaSurface(sur, getTextWidth()+4, 0);
if (isSelected()) sur->dispose();
bg = theme->colors.listitemSelectedFace();
else
bg = theme->colors.listitemNormalFace();
g->fillRect(bg, getClientBounds());
g->drawRgbaSurface(sur, 0, 0);
} }
} }
void onPreferredSize(PreferredSizeEvent& ev) override { void onPreferredSize(PreferredSizeEvent& ev) override {
if (m_image)
ev.setPreferredSize(m_image->width(),
m_image->height());
else
ListItem::onPreferredSize(ev); ListItem::onPreferredSize(ev);
if (m_image) {
gfx::Size sz = ev.getPreferredSize();
ev.setPreferredSize(
sz.w + 4 + m_image->width(),
MAX(sz.h, m_image->height()));
}
} }
void onSelect() override { void onSelect() override {
@ -97,10 +92,11 @@ private:
gfx::Color color = theme->colors.text(); gfx::Color color = theme->colors.text();
try { try {
m_loaded = true;
m_image.reset( m_image.reset(
render_text( render_text(
m_filename, 16, m_filename, 16,
getText(), "ABCDEabcde", // TODO custom text
doc::rgba(gfx::getr(color), doc::rgba(gfx::getr(color),
gfx::getg(color), gfx::getg(color),
gfx::getb(color), gfx::getb(color),
@ -111,13 +107,14 @@ private:
listbox->makeChildVisible(this); listbox->makeChildVisible(this);
} }
catch (const std::exception& ex) { catch (const std::exception& ex) {
Console::showException(ex); // Ignore errors
} }
} }
private: private:
base::UniquePtr<doc::Image> m_image; base::UniquePtr<doc::Image> m_image;
std::string m_filename; std::string m_filename;
bool m_loaded;
}; };
FontPopup::FontPopup() FontPopup::FontPopup()