0

I'm creating a C++ wxWidgets calculator application. I'm implementing trigonometric functions, and to save on space I've reunited all of them in a single button. If you right click on the button then, a popup is created, which contains buttons for all the functions. I'm using a derived wxPopupTransientWindow class for this job, the problem is, the buttons aren't displaying correctly.

Popup problem

This is my code:

expandMenu.h

#include "wx/wx.h"
#include "wx/popupwin.h"

struct expandMenuInfo
{
    const wxString& label;
    wxWindowID id;

    expandMenuInfo(const wxString& l, wxWindowID i)
        : label{l}
        , id{i}
    {}
};

class expandMenu : public wxPopupTransientWindow
{
    wxWindow* panel;
    wxBoxSizer* sizer;

public:
    expandMenu(wxWindow* parent, wxPoint pos, std::vector<expandMenuInfo> buttons);
    ~expandMenu();
};

expandMenu.cpp

#include "expandMenu.h"

// PANNELLO ESTENSIONE

expandMenu::expandMenu(wxWindow* parent, wxPoint pos, std::vector<expandMenuInfo> buttons)
    : wxPopupTransientWindow(parent, wxBORDER_NONE | wxPU_CONTAINS_CONTROLS)
{
    this->SetPosition(pos);
    this->SetSize(50 * buttons.size(), 50);

    this->SetBackgroundColour(wxColour(90, 93, 121));

    panel = new wxWindow(this, wxID_ANY);
    sizer = new wxBoxSizer(wxHORIZONTAL);

    // costruisci struttura
    for (unsigned int i = 0; i < buttons.size(); i++)
    {
        wxButton* btn = new wxButton(this, buttons.at(i).id, buttons.at(i).label);
        sizer->Add(btn, 1, wxEXPAND);
    }
    panel->SetSizer(sizer);
}

expandMenu::~expandMenu()
{
}

And this is the code I use for my custom wxButton's to actually create the popup (it's temporary):

void ikeButton::rightClick(wxMouseEvent& evt) // CREA PANNELLO ESTENSIONE
{
    if (flags & EXPANDABLE)
    {
        std::vector<expandMenuInfo> buttons;
        buttons.push_back(expandMenuInfo(L"sin", 3001));
        buttons.push_back(expandMenuInfo(L"cos", 3002));
        buttons.push_back(expandMenuInfo(L"tan", 3003));

        expandMenu* menu = new expandMenu(this, wxGetMousePosition(), buttons);
        menu->Popup();
    }
}

Thank you in advance for any help, as I'm pretty new to this framework.

3
  • why? Can't you just create wxComboBox for that?
    – Igor
    Jun 19, 2021 at 16:09
  • I want the sine button to do its thing on left click and to open the menu on right click. The buttons inside the menu need to be placed in a grid, and when one of them is selected, I want the sine button to turn into the button that was pressed. Is all of this possible to achieve with a single wxComboBox?
    – iKebab897
    Jun 19, 2021 at 17:32
  • I'm thinking of using wxComboBox to select the appropriate functions. Please check the docs for the control screenshot and widgets sample for control behavior.
    – Igor
    Jun 19, 2021 at 21:06

2 Answers 2

2

To get the buttons to layout in the popup window, in the constuctor for expandMenu I think you just need to change panel->SetSizer(sizer); to

    SetSizerAndFit(sizer);
    Layout();

From a UI perspective, I think a split button might be a better way to implement the functionality you are describing. wxWidgets doesn't currently have such a control, but this post shows 2 different ways you can create one.

0

I believe the issue is that you're specifying this for the parent of the buttons, i.e. the popup window. I think you wanted their parent to be the panel instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.