Project

General

Profile

Improvements #13814 ยป wt_WMenuItem_WIcon.patch

Christian Meyer, 06/30/2025 09:43 PM

View differences:

src/Wt/WMenuItem.C
#include "Wt/WStackedWidget.h"
#include "Wt/WText.h"
#include "Wt/WTheme.h"
#include "Wt/WIcon.h"
#include "StdWidgetItemImpl.h"
......
create(iconPath, text, std::move(contents), policy);
}
WMenuItem::WMenuItem(std::unique_ptr<WIcon> wicon, const WString& text,
std::unique_ptr<WWidget> 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)
{
......
text_ = nullptr;
icon_ = nullptr;
wIcon_ = nullptr;
checkBox_ = nullptr;
subMenu_ = nullptr;
data_ = nullptr;
......
icon_->decorationStyle().setBackgroundImage(WLink(path));
}
WIcon* WMenuItem::setIcon(std::unique_ptr<WIcon> 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_)
......
return std::string();
}
WIcon* WMenuItem::wIcon() const
{
// its either set or nullptr
return wIcon_;
}
void WMenuItem::setText(const WString& text)
{
if (!text_) {
src/Wt/WMenuItem.h
std::unique_ptr<WWidget> 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<WIcon> iconPtr, const WString& label,
std::unique_ptr<WWidget> contents = nullptr,
ContentLoading policy = ContentLoading::Lazy);
/* !\brief Destructor.
*
* Removes the item from the menu (if it was added previously to one), and
......
* 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<WIcon> 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
......
/*! \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<WText>("<i class=\"bi bi-play\"></i>");
* iconText->setTextFormat(TextFormat::XHTML);
* item->anchor()->insertWidget(0, std::move(iconText));
* \endcode
*
*/
WAnchor *anchor() const;
......
WMenu *menu_, *subMenu_;
WText *icon_;
WIcon *wIcon_;
WLabel *text_;
WCheckBox *checkBox_;
void *data_;
    (1-1/1)