Ardour  9.0-pre0-822-g12e3bc5c20
metabutton.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2025 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <ytkmm/menu.h>
22 #include <ytkmm/menuitem.h>
23 
24 #include "widgets/ardour_button.h"
25 #include "widgets/visibility.h"
26 
27 namespace ArdourWidgets {
28 
30 {
31 public:
33  virtual ~MetaButton ();
34 
35  void add_item (std::string const& label, std::string const& menutext, sigc::slot<void> const&);
36  void clear_items ();
37 
38  void set_active (std::string const&);
39  void set_index (guint);
40 
41  guint index () const
42  {
43  return _active;
44  }
45 
46 protected:
47  bool on_button_press_event (GdkEventButton*);
48  bool on_motion_notify_event (GdkEventMotion*);
49  void menu_size_request (Gtk::Requisition*);
50  void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*);
51 
52 private:
53  class MetaMenuItem : public Gtk::MenuItem
54  {
55  public:
56  MetaMenuItem (std::string const& label, std::string const& menutext, sigc::slot<void> const& cb)
57  : Gtk::MenuItem (menutext, false)
58  , _label (label)
59  , _cb (cb)
60  {
61  }
62 
63  void activate () const
64  {
65  _cb ();
66  }
67 
68  std::string label () const
69  {
70  return _label;
71  }
72 
73  std::string menutext () const
74  {
75  return get_label ();
76  }
77 
78  private:
79  std::string _label;
80  sigc::slot<void> _cb;
81  };
82 
83  class MetaElement : public Gtk::Menu_Helpers::MenuElem
84  {
85  public:
86  typedef sigc::slot<void, MetaMenuItem const*> SlotActivate;
87 
88  MetaElement (std::string const& label, std::string const& menutext, sigc::slot<void> const& cb, SlotActivate const& wrap)
89  : Gtk::Menu_Helpers::MenuElem ("")
90  {
91  MetaMenuItem* mmi = manage (new MetaMenuItem (label, menutext, cb));
92  child_->unreference ();
93  set_child (mmi);
94  child_->signal_activate ().connect (sigc::bind (wrap, mmi));
95  child_->show ();
96  }
97  };
98 
99  void activate_item (MetaMenuItem const*);
101 
102  Gtk::Menu _menu;
103  guint _active;
105 };
106 
107 } // namespace ArdourWidgets
MetaElement(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb, SlotActivate const &wrap)
Definition: metabutton.h:88
sigc::slot< void, MetaMenuItem const * > SlotActivate
Definition: metabutton.h:86
MetaMenuItem(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb)
Definition: metabutton.h:56
void set_active(std::string const &)
bool on_motion_notify_event(GdkEventMotion *)
void menu_size_request(Gtk::Requisition *)
void add_item(std::string const &label, std::string const &menutext, sigc::slot< void > const &)
void render(Cairo::RefPtr< Cairo::Context > const &, cairo_rectangle_t *)
bool on_button_press_event(GdkEventButton *)
void update_button(MetaMenuItem const *)
void activate_item(MetaMenuItem const *)
Definition: ardour_ui.h:189
#define LIBWIDGETS_API