diff --git a/src/Wt/WMenuItem.C b/src/Wt/WMenuItem.C index 9f52d804..99889dac 100644 --- a/src/Wt/WMenuItem.C +++ b/src/Wt/WMenuItem.C @@ -19,6 +19,7 @@ #include "Wt/WStackedWidget.h" #include "Wt/WText.h" #include "Wt/WTheme.h" +#include "Wt/WIcon.h" #include "StdWidgetItemImpl.h" @@ -42,6 +43,14 @@ WMenuItem::WMenuItem(const std::string& iconPath, const WString& text, create(iconPath, text, std::move(contents), policy); } +WMenuItem::WMenuItem(std::unique_ptr wicon, const WString& text, + std::unique_ptr contents, ContentLoading policy) + : separator_(false) +{ + create(std::string(), text, std::move(contents), policy); + setIcon(std::move(wicon)); +} + WMenuItem::WMenuItem(bool separator, const WString& text) : separator_(true) { @@ -73,6 +82,7 @@ void WMenuItem::create(const std::string& iconPath, const WString& text, text_ = nullptr; icon_ = nullptr; + wIcon_ = nullptr; checkBox_ = nullptr; subMenu_ = nullptr; data_ = nullptr; @@ -162,6 +172,32 @@ void WMenuItem::setIcon(const std::string& path) icon_->decorationStyle().setBackgroundImage(WLink(path)); } +WIcon* WMenuItem::setIcon(std::unique_ptr iconPtr) +{ + if (!wIcon_) { + WAnchor *a = anchor(); + if (!a) + return nullptr; + + wIcon_ = a->insertWidget(0, std::move(iconPtr)); + } + else + { + WAnchor *a = anchor(); + // if the Icon exists, so should the anchor() + + a->removeWidget(wIcon_); + wIcon_ = a->insertWidget(0, std::move(iconPtr)); + } + + // Setting the theme misaligns the Icon. But there is also no distance to the icon. + // distance classes can be used in the Icon itself (bootstrap5: me-2) + // WApplication *app = WApplication::instance(); + // app->theme()->apply(this, wIcon_, MenuItemIcon); + + return wIcon_; +} + std::string WMenuItem::icon() const { if (icon_) @@ -170,6 +206,12 @@ std::string WMenuItem::icon() const return std::string(); } +WIcon* WMenuItem::wIcon() const +{ + // its either set or nullptr + return wIcon_; +} + void WMenuItem::setText(const WString& text) { if (!text_) { diff --git a/src/Wt/WMenuItem.h b/src/Wt/WMenuItem.h index feff4500..94cf8f39 100644 --- a/src/Wt/WMenuItem.h +++ b/src/Wt/WMenuItem.h @@ -54,6 +54,16 @@ public: std::unique_ptr contents = nullptr, ContentLoading policy = ContentLoading::Lazy); + + /* + * The icon is displayed left to the text. + * + * \note The Icon is created with Webfonts. + */ + WMenuItem(std::unique_ptr iconPtr, const WString& label, + std::unique_ptr contents = nullptr, + ContentLoading policy = ContentLoading::Lazy); + /* !\brief Destructor. * * Removes the item from the menu (if it was added previously to one), and @@ -85,15 +95,31 @@ public: * The icon should have a width of 16 pixels. * * \sa setText() + * + * \note To use xhtml icons use \sa anchor() to insert WText */ void setIcon(const std::string& path); + /*! \brief Sets the item icon. + *. + * \sa setText() + * + * \note To use xhtml icons use \sa anchor() to insert WText + */ + WIcon* setIcon(std::unique_ptr iconPtr); + /*! \brief Returns the item icon path. * * \sa setIcon() */ std::string icon() const; + /*! \brief Returns the item pointer. + * + * \sa setIcon() + */ + WIcon* wIcon() const; + /*! \brief Sets if the item is checkable. * * When an item is checkable, a checkbox is displayed to the left of the @@ -332,7 +358,15 @@ public: /*! \brief Returns the anchor of this menu item. * - * Can be used to add widgets to the menu. + * Can be used to add widgets to the item. + * + * If you want to use xhtml Items like Icons: + * \code {.cpp} + * auto iconText = std::make_unique(""); + * iconText->setTextFormat(TextFormat::XHTML); + * item->anchor()->insertWidget(0, std::move(iconText)); + * \endcode + * */ WAnchor *anchor() const; @@ -371,6 +405,7 @@ private: WMenu *menu_, *subMenu_; WText *icon_; + WIcon *wIcon_; WLabel *text_; WCheckBox *checkBox_; void *data_;