1/*
2 src/popupbutton.cpp -- Button which launches a popup widget
3
4 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
5 The widget drawing code is based on the NanoVG demo application
6 by Mikko Mononen.
7
8 All rights reserved. Use of this source code is governed by a
9 BSD-style license that can be found in the LICENSE.txt file.
10*/
11
12#include <nanogui/popupbutton.h>
13#include <nanogui/theme.h>
14#include <nanogui/opengl.h>
15#include <nanogui/serializer/core.h>
16
17NAMESPACE_BEGIN(nanogui)
18
19PopupButton::PopupButton(Widget *parent, const std::string &caption, int buttonIcon)
20 : Button(parent, caption, buttonIcon) {
21
22 mChevronIcon = mTheme->mPopupChevronRightIcon;
23
24 setFlags(Flags::ToggleButton | Flags::PopupButton);
25
26 Window *parentWindow = window();
27 mPopup = new Popup(parentWindow->parent(), window());
28 mPopup->setSize(Vector2i(320, 250));
29 mPopup->setVisible(false);
30
31 mIconExtraScale = 0.8f;// widget override
32}
33
34PopupButton::~PopupButton() {
35 mPopup->setVisible(false);
36}
37
38Vector2i PopupButton::preferredSize(NVGcontext *ctx) const {
39 return Button::preferredSize(ctx) + Vector2i(15, 0);
40}
41
42void PopupButton::draw(NVGcontext* ctx) {
43 if (!mEnabled && mPushed)
44 mPushed = false;
45
46 mPopup->setVisible(mPushed);
47 Button::draw(ctx);
48
49 if (mChevronIcon) {
50 auto icon = utf8(mChevronIcon);
51 NVGcolor textColor =
52 mTextColor.w() == 0 ? mTheme->mTextColor : mTextColor;
53
54 nvgFontSize(ctx, (mFontSize < 0 ? mTheme->mButtonFontSize : mFontSize) * icon_scale());
55 nvgFontFace(ctx, "icons");
56 nvgFillColor(ctx, mEnabled ? textColor : mTheme->mDisabledTextColor);
57 nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
58
59 float iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr);
60 Vector2f iconPos(0, mPos.y() + mSize.y() * 0.5f - 1);
61
62 if (mPopup->side() == Popup::Right)
63 iconPos[0] = mPos.x() + mSize.x() - iw - 8;
64 else
65 iconPos[0] = mPos.x() + 8;
66
67 nvgText(ctx, iconPos.x(), iconPos.y(), icon.data(), nullptr);
68 }
69}
70
71void PopupButton::performLayout(NVGcontext *ctx) {
72 Widget::performLayout(ctx);
73
74 const Window *parentWindow = window();
75
76 int posY = absolutePosition().y() - parentWindow->position().y() + mSize.y() /2;
77 if (mPopup->side() == Popup::Right)
78 mPopup->setAnchorPos(Vector2i(parentWindow->width() + 15, posY));
79 else
80 mPopup->setAnchorPos(Vector2i(0 - 15, posY));
81}
82
83void PopupButton::setSide(Popup::Side side) {
84 if (mPopup->side() == Popup::Right &&
85 mChevronIcon == mTheme->mPopupChevronRightIcon)
86 setChevronIcon(mTheme->mPopupChevronLeftIcon);
87 else if (mPopup->side() == Popup::Left &&
88 mChevronIcon == mTheme->mPopupChevronLeftIcon)
89 setChevronIcon(mTheme->mPopupChevronRightIcon);
90 mPopup->setSide(side);
91}
92
93void PopupButton::save(Serializer &s) const {
94 Button::save(s);
95 s.set("chevronIcon", mChevronIcon);
96}
97
98bool PopupButton::load(Serializer &s) {
99 if (!Button::load(s))
100 return false;
101 if (!s.get("chevronIcon", mChevronIcon))
102 return false;
103 return true;
104}
105
106NAMESPACE_END(nanogui)
107