gansm / finalcut

A text-based widget toolkit
https://github.com/gansm/finalcut/wiki/First-steps#first-steps-with-the-final-cut-widget-toolkit
GNU Lesser General Public License v3.0
981 stars 52 forks source link

is there a right click menu plugin ? #102

Closed alavaelian closed 2 years ago

alavaelian commented 2 years ago

Feature Request

there is a widget to make right click menu ?

gansm commented 2 years ago

Sorry for waiting so long for my reply. I was on vacation. You can easily implement a right-click menu with an FMenu widget. I have a small example for you here:

#include <final/final.h>

using namespace finalcut;

class ClickableLabel : public FLabel
{
  public:
    explicit ClickableLabel (FWidget* parent = nullptr)
      : FLabel{parent}
    {
      init();
    }

    explicit ClickableLabel (const FString& txt, FWidget* parent = nullptr)
      : FLabel{txt, parent}
    {
      init();
    }

    void onMouseDown (FMouseEvent* ev) override
    {
      if ( ev->getButton() != MouseButton::Right )
        return;

      openMenu ({ev->getTermX(), ev->getTermY()});
    }

  private:
    void init()
    {
      auto cb_tooltip = \
      [] (const auto& string)
      {
        auto text = string.toWString();
        auto new_end = std::remove(std::begin(text), std::end(text), L'&');
        text.erase(new_end, std::end(text));
        finalcut::FToolTip tooltip(text, getMainWidget());
        tooltip.show();
        sleep(2);
      };

      item1.addCallback ("clicked", cb_tooltip, item1.getText());
      item2.addCallback ("clicked", cb_tooltip, item2.getText());
      item3.addCallback ("clicked", cb_tooltip, item3.getText());
      item4.addCallback ("clicked", cb_tooltip, item4.getText());
    }

    void openMenu (FPoint pos)
    {
      if ( context_menu.isShown() )
      {
        leaveMenu();
      }
      else
      {
        FWidget::setOpenMenu(&context_menu);
        context_menu.setPos (pos);
        context_menu.setVisible();
        context_menu.show();
        context_menu.raiseWindow();
        context_menu.redraw();
      }
    }

    void leaveMenu()
    {
      context_menu.unselectItem();
      context_menu.hide();
      auto win_widget = FWindow::getWindowWidget(this);

      if ( ! win_widget )
        return;

      win_widget->activateWindow();
      win_widget->raiseWindow();

      if ( win_widget->getWindowFocusWidget() )
        win_widget->getWindowFocusWidget()->setFocus();

      redraw();

      if ( getStatusBar() )
        getStatusBar()->drawMessage();
    }

    FMenu      context_menu{"menu", this};
    FMenuItem  item1{"Item &1", &context_menu};
    FMenuItem  item2{"Item &2", &context_menu};
    FMenuItem  item3{"Item &3", &context_menu};
    FMenuItem  item4{"Item &4", &context_menu};
};

class dialogWidget : public FDialog
{
  public:
    explicit dialogWidget (FWidget* parent = nullptr)
      : FDialog{parent}
    { }

  private:
    void initLayout()
    {
      setText ("Dialog");
      setGeometry (FPoint{30, 8}, FSize{20, 5});
      label.setAlignment (Align::Center);
      label.setGeometry (FPoint{1, 1}, FSize{18, 2});
      FDialog::initLayout();
    }

    ClickableLabel label{"Please right\nclick here", this};
};

int main (int argc, char* argv[])
{
  FApplication app(argc, argv);
  dialogWidget dialog(&app);
  FWidget::setMainWidget(&dialog);
  dialog.show();
  return app.exec();
}

image

(Please keep in mind that not every terminal has mouse support)

alavaelian commented 2 years ago

thanks for your answer and btw are you a wizard of coding man ? i think your are very talented

gansm commented 2 years ago

I'm not a wizard. I only have the power to use magic that others have created.

alavaelian commented 2 years ago

that is still amazing power