Ardour  9.0-pre0-844-g046623df59
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 add_item (std::string const& label, std::string const& menutext, Gtk::Menu&, sigc::slot<void> const&);
37  void clear_items ();
38 
39  void set_active (std::string const&);
40  void set_index (guint);
41  void set_by_menutext (std::string const & mt);
42 
43  guint index () const
44  {
45  return _active;
46  }
47 
48  Gtk::Menu& menu() { return _menu; }
49 
50  bool is_menu_popup_event (GdkEventButton* ev) const;
51 
52 protected:
53  bool on_button_press_event (GdkEventButton*);
54  bool on_motion_notify_event (GdkEventMotion*);
55  void menu_size_request (Gtk::Requisition*);
56  void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*);
57 
58 private:
59  class MetaMenuItem : public Gtk::MenuItem
60  {
61  public:
62  MetaMenuItem (std::string const& label, std::string const& menutext, sigc::slot<void> const& cb)
63  : Gtk::MenuItem (menutext, false)
64  , _label (label)
65  , _cb (cb)
66  {
67  }
68 
69  MetaMenuItem (std::string const& label, std::string const& menutext, sigc::slot<void> const & cb, Gtk::Menu& submenu)
70  : Gtk::MenuItem (menutext, false)
71  , _label (label)
72  , _cb (cb)
73  {
74  set_submenu (submenu);
75  }
76 
77  void activate () const
78  {
79  _cb ();
80  }
81 
82  std::string label () const
83  {
84  return _label;
85  }
86 
87  std::string menutext () const
88  {
89  return get_label ();
90  }
91 
92  private:
93  std::string _label;
94  sigc::slot<void> _cb;
95  };
96 
97  class MetaElement : public Gtk::Menu_Helpers::MenuElem
98  {
99  public:
100  typedef sigc::slot<void, MetaMenuItem const*> SlotActivate;
101 
102  MetaElement (std::string const& label, std::string const& menutext, sigc::slot<void> const& cb, SlotActivate const& wrap)
103  : Gtk::Menu_Helpers::MenuElem ("")
104  {
105  MetaMenuItem* mmi = manage (new MetaMenuItem (label, menutext, cb));
106  child_->unreference ();
107  set_child (mmi);
108  child_->signal_activate ().connect (sigc::bind (wrap, mmi));
109  child_->show ();
110  }
111  MetaElement (std::string const& label, std::string const & menutext, sigc::slot<void> const & cb, SlotActivate const & wrap, Gtk::Menu& submenu)
112  : Gtk::Menu_Helpers::MenuElem ("")
113  {
114  MetaMenuItem* mmi = manage (new MetaMenuItem (label, menutext, cb, submenu));
115  child_->unreference ();
116  set_child (mmi);
117  child_->signal_activate ().connect (sigc::bind (wrap, mmi));
118  child_->show ();
119  }
120 
121  };
122 
125 
126  Gtk::Menu _menu;
127  guint _active;
129 };
130 
131 } // namespace ArdourWidgets
MetaElement(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb, SlotActivate const &wrap)
Definition: metabutton.h:102
sigc::slot< void, MetaMenuItem const * > SlotActivate
Definition: metabutton.h:100
MetaElement(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb, SlotActivate const &wrap, Gtk::Menu &submenu)
Definition: metabutton.h:111
MetaMenuItem(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb, Gtk::Menu &submenu)
Definition: metabutton.h:69
MetaMenuItem(std::string const &label, std::string const &menutext, sigc::slot< void > const &cb)
Definition: metabutton.h:62
void add_item(std::string const &label, std::string const &menutext, Gtk::Menu &, sigc::slot< void > const &)
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 set_by_menutext(std::string const &mt)
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 *)
bool is_menu_popup_event(GdkEventButton *ev) const
Definition: ardour_ui.h:189
#define LIBWIDGETS_API