diff --git a/src/Wt/WIcon.C b/src/Wt/WIcon.C index fa356cec..7dce7cee 100644 --- a/src/Wt/WIcon.C +++ b/src/Wt/WIcon.C @@ -16,11 +16,21 @@ namespace Wt { WIcon::WIcon() - : iconChanged_(false) + : iconChanged_(false), iconSet_(IconFamily::FontAwesome) +{ } + +WIcon::WIcon(IconFamily iconFam) + : iconChanged_(false), iconSet_(iconFam) { } WIcon::WIcon(const std::string& name) - : iconChanged_(false) + : iconChanged_(false), iconSet_(IconFamily::FontAwesome) +{ + setName(name); +} + +WIcon::WIcon(const std::string& name, IconFamily iconFam) + : iconChanged_(false), iconSet_(iconFam) { setName(name); } @@ -37,6 +47,16 @@ void WIcon::setName(const std::string& name) } } +void WIcon::setIconFamily(IconFamily iconFam) +{ + if(iconSet_ != iconFam) + { + iconSet_ = iconFam; + iconChanged_ = true; + repaint(); + } +} + void WIcon::setSize(double factor) { decorationStyle().font().setSize(WLength(factor, LengthUnit::FontEm)); @@ -59,7 +79,21 @@ void WIcon::updateDom(DomElement& element, bool all) sc = styleClass().toUTF8(); if (!name_.empty()) - sc = Utils::addWord(sc, "fa fa-" + name_); + { + switch (iconSet_) + { + case IconFamily::FontAwesome: + sc = Utils::addWord(sc, "fa fa-" + name_); + break; + case IconFamily::Bootstrap: + sc = Utils::addWord(sc, "bi bi-" + name_); + break; + + default: + sc = Utils::addWord(sc, "fa fa-" + name_); + break; + } + } element.setProperty(Property::Class, sc); diff --git a/src/Wt/WIcon.h b/src/Wt/WIcon.h index b03aa835..84df00cc 100644 --- a/src/Wt/WIcon.h +++ b/src/Wt/WIcon.h @@ -11,8 +11,19 @@ namespace Wt { + /** + * @brief Enum to switch between Icon Families + * + */ + enum class IconFamily + { + FontAwesome, + Bootstrap + }; + /*! \class WIcon Wt/WIcon.h Wt/WIcon.h - * \brief A widget that represents a Font-Aweswome icon. + * \brief A widget that represents a Font-Aweswome icon but can also used to + * represent Bootstrap Icons. * * By default, Wt will load the default Font-Awesome included with it. * This is version 4.3.0. For a list of all icons, visit: @@ -23,21 +34,31 @@ namespace Wt { class WT_API WIcon : public WInteractWidget { public: - //! Creates an empty icon. + // ! Creates an empty icon. WIcon(); + //! Creates an empty icon with a Icon Family Set + WIcon(IconFamily iconFam); + /*! \brief Creates an icon with the given name. - * + * Defaults to IconFamily Font-Awesome * \see setName() */ WIcon(const std::string& name); + /*! \brief Creates an icon with the given name and IconFamily. + * + * \see setName() + */ + WIcon(const std::string& name, IconFamily iconFam); + /*! \brief Set the icon name. * * This sets the name of the icon. The name should be a the name of a * Font-Aweswome icon, without the `fa-` prefix. + * For Bootstrap Icons, without the `bi-` prefix. * - * Usage example: + * Usage example Font-Awesome: * The "play" icon: https://fontawesome.com/v4/icon/play * can be included with: * \if cpp @@ -52,9 +73,28 @@ public: * \endcode * \endif * + * + * Usage example Bootstrap Icons: + * The "play" icon: https://icons.getbootstrap.com/icons/play/ + * can be included with: + * \if cpp + * \code + * auto app = Wt::WApplication::instance(); + * app->root()->addNew("play", IconFamily::Bootstrap); + * \endcode + * \elseif java + * \code + * WApplication app = WApplication.getInstance(); + * app.getRoot().addWidget(new WIcon("play", IconFamily::Bootstrap)); + * \endcode + * \endif + * * \note The name can be followed by sizing information * separated by a space if the Font Aweswome version - * used allows it. E.g. \p "play fa-4" + * used allows it. E.g. \p "play fa-4". + * + * \note For Bootstrap Icons, additional style classes can be set after the name. + * E.g. \p "play opacity-50" */ void setName(const std::string& name); @@ -64,6 +104,19 @@ public: */ std::string name() const { return name_; } + /*! \brief Set the icon Family. + * + * This sets the Family of the icon. + * + */ + void setIconFamily(IconFamily iconFam); + + /*! \brief Returns the icon family. + * + * \see setIconFamily() + */ + IconFamily iconFamily() const { return iconSet_; } + /*! \brief Changes the icon's size. * * \note This is done in CSS, not using the `fa-{size}` method. @@ -100,6 +153,7 @@ protected: private: std::string name_; bool iconChanged_; + IconFamily iconSet_; }; }