/* src/button.cpp -- [Normal/Toggle/Radio/Popup] Button widget NanoGUI was developed by Wenzel Jakob . The widget drawing code is based on the NanoVG demo application by Mikko Mononen. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE.txt file. */ #include #include #include #include NAMESPACE_BEGIN(nanogui) Button::Button(Widget *parent, const std::string &caption, int icon) : Widget(parent), mCaption(caption), mIcon(icon), mIconPosition(IconPosition::LeftCentered), mPushed(false), mFlags(NormalButton), mBackgroundColor(Color(0, 0)), mTextColor(Color(0, 0)) { } Vector2i Button::preferredSize(NVGcontext *ctx) const { int fontSize = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize; nvgFontSize(ctx, fontSize); nvgFontFace(ctx, "sans-bold"); float tw = nvgTextBounds(ctx, 0,0, mCaption.c_str(), nullptr, nullptr); float iw = 0.0f, ih = fontSize; if (mIcon) { if (nvgIsFontIcon(mIcon)) { ih *= icon_scale(); nvgFontFace(ctx, "icons"); nvgFontSize(ctx, ih); iw = nvgTextBounds(ctx, 0, 0, utf8(mIcon).data(), nullptr, nullptr) + mSize.y() * 0.15f; } else { int w, h; ih *= 0.9f; nvgImageSize(ctx, mIcon, &w, &h); iw = w * ih / h; } } return Vector2i((int)(tw + iw) + 20, fontSize + 10); } bool Button::mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) { Widget::mouseButtonEvent(p, button, down, modifiers); /* Temporarily increase the reference count of the button in case the button causes the parent window to be destructed */ ref