Ardour  9.0-pre0-844-g046623df59
ardour_button.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017-2018 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef _WIDGETS_ARDOUR_BUTTON_H_
21 #define _WIDGETS_ARDOUR_BUTTON_H_
22 
23 #include <list>
24 #include <stdint.h>
25 
26 #include <ytkmm/action.h>
27 
28 #include "pbd/signals.h"
29 #include "gtkmm2ext/activatable.h"
30 #include "gtkmm2ext/cairo_widget.h"
31 
32 #include "widgets/ardour_icon.h"
33 #include "widgets/binding_proxy.h"
34 #include "widgets/visibility.h"
35 
36 namespace ArdourWidgets {
37 
39 {
40  public:
41  enum Element {
42  Edge = 0x001,
43  Body = 0x002,
44  Text = 0x004,
45  Indicator = 0x008,
46  ColorBox = 0x018, // also sets Indicator
47  Menu = 0x020,
48  MetaMenu = 0x040,
49  Inactive = 0x080, // no _action is defined AND state is not used
50  VectorIcon = 0x100,
51  IconRenderCallback = 0x200,
52  };
53 
54  typedef void (* rendercallback_t) (cairo_t*, int, int, uint32_t, void*);
55 
59 
60  ArdourButton (Element e = default_elements, bool toggle = false);
61  ArdourButton (const std::string&, Element e = default_elements, bool toggle = false);
62  virtual ~ArdourButton ();
63 
64  enum Tweaks {
65  Square = 0x01,
66  TrackHeader = 0x02,
67  OccasionalText = 0x04,
68  OccasionalLED = 0x08,
69  ForceBoxy = 0x10,
70  ForceFlat = 0x20,
71  ExpandtoSquare = 0x40,
72  };
73 
75  static void set_default_tweaks (Tweaks t) { default_tweaks = t; }
76 
77  Tweaks tweaks() const { return _tweaks; }
79 
82 
83  void set_custom_led_color (const uint32_t c, const bool useit = true);
84 
85  void set_act_on_release (bool onRelease) { _act_on_release = onRelease; }
86 
87  Element elements() const { return _elements; }
91 
92  ArdourIcon::Icon icon() const { return _icon; }
94  void set_icon (rendercallback_t, void*);
95 
96  void set_corner_radius (float);
97 
98  void set_text (const std::string&, bool markup = false);
99  const std::string& get_text () const { return _text; }
100  bool get_markup () const { return _markup; }
101  void set_angle (const double);
102  void set_alignment (const float, const float);
103  void get_alignment (float& xa, float& ya) {xa = _xalign; ya = _yalign;};
104 
105  void set_led_left (bool yn);
106  void set_distinct_led_click (bool yn);
107 
109  void set_layout_font (const Pango::FontDescription&);
110  void set_text_ellipsize (Pango::EllipsizeMode);
111  void set_width_padding (float);
112 
113  /* Sets the text used for size request computation. Pass an
114  * empty string to return to the default behavior which uses
115  * the currently displayed text for measurement. */
116  void set_sizing_text (std::string const&);
117  void add_sizing_text (std::string const&);
118  void set_sizing_texts (std::vector<std::string> const&);
119 
120  bool is_led_click (GdkEventButton*);
121  sigc::signal<void, GdkEventButton*> signal_led_clicked;
122  sigc::signal<void> signal_clicked;
123 
124  std::shared_ptr<PBD::Controllable> get_controllable() { return binding_proxy.get_controllable(); }
125  void set_controllable (std::shared_ptr<PBD::Controllable> c);
126  void watch ();
127 
128  void set_related_action (Glib::RefPtr<Gtk::Action>);
129 
130  bool on_button_press_event (GdkEventButton*);
131  bool on_button_release_event (GdkEventButton*);
132  bool on_touch_begin_event (GdkEventTouch*);
133  bool on_touch_end_event (GdkEventTouch*);
134 
135  void set_image (const Glib::RefPtr<Gdk::Pixbuf>&);
136 
137  void set_fixed_colors (const uint32_t active_color, const uint32_t inactive_color);
138  void set_active_color (const uint32_t active_color);
139  void set_inactive_color (const uint32_t inactive_color);
141 
142  void set_fallthrough_to_parent(bool fall) { _fallthrough_to_parent = fall; }
143 
144  unsigned int char_pixel_width() { if (_char_pixel_width < 1) recalc_char_pixel_geometry() ; return _char_pixel_width; }
145  unsigned int char_pixel_height() { if (_char_pixel_height < 1) recalc_char_pixel_geometry() ; return _char_pixel_height; }
146  float char_avg_pixel_width() { if (_char_pixel_width < 1) recalc_char_pixel_geometry() ; return _char_avg_pixel_width; }
147 
148  protected:
149  void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*);
150  void on_size_request (Gtk::Requisition* req);
151  void on_size_allocate (Gtk::Allocation&);
152  void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
154  void on_realize ();
155  bool on_enter_notify_event (GdkEventCrossing*);
156  bool on_leave_notify_event (GdkEventCrossing*);
157  bool on_grab_broken_event(GdkEventGrabBroken*);
158  bool on_focus_in_event (GdkEventFocus*);
159  bool on_focus_out_event (GdkEventFocus*);
160  bool on_key_release_event (GdkEventKey *);
161  bool on_key_press_event (GdkEventKey *);
162 
165 
166  protected:
167  Glib::RefPtr<Pango::Layout> _layout;
168  Glib::RefPtr<Gdk::Pixbuf> _pixbuf;
169  std::string _text;
170  std::vector<std::string> _sizing_texts;
171  bool _markup;
174  rendercallback_t _icon_render_cb;
178 
181 
182  unsigned int _char_pixel_width;
183  unsigned int _char_pixel_height;
187 
190  float _diameter;
193 
194  double _angle;
195  float _xalign, _yalign;
196 
199 
202 
207 
208  uint32_t outline_color;
209 
210  std::optional<int> _squaresize;
211 
212  cairo_pattern_t* convex_pattern;
213  cairo_pattern_t* concave_pattern;
214  cairo_pattern_t* led_inset_pattern;
215  cairo_rectangle_t* _led_rect;
216 
219  bool _led_left;
221  bool _hovering;
222  bool _focused;
226  Pango::EllipsizeMode _ellipsis;
229 
230  void setup_led_rect ();
231  void set_colors ();
232  void color_handler ();
233  void build_patterns ();
234  void ensure_layout ();
235 
236  void action_toggled ();
240 };
241 
242 } /* end namespace */
243 
244 #endif
void set_icon(rendercallback_t, void *)
rendercallback_t _icon_render_cb
bool on_grab_broken_event(GdkEventGrabBroken *)
void set_sizing_text(std::string const &)
void set_image(const Glib::RefPtr< Gdk::Pixbuf > &)
void set_active_state(Gtkmm2ext::ActiveState)
void set_angle(const double)
bool on_touch_begin_event(GdkEventTouch *)
void set_act_on_release(bool onRelease)
Definition: ardour_button.h:85
static Element led_default_elements
Definition: ardour_button.h:57
void add_sizing_text(std::string const &)
void on_style_changed(const Glib::RefPtr< Gtk::Style > &)
cairo_pattern_t * concave_pattern
Glib::RefPtr< Pango::Layout > _layout
void set_inactive_color(const uint32_t inactive_color)
Glib::RefPtr< Gdk::Pixbuf > _pixbuf
PBD::ScopedConnection watch_connection
unsigned int char_pixel_width()
sigc::signal< void, GdkEventButton * > signal_led_clicked
bool on_focus_in_event(GdkEventFocus *)
bool on_key_press_event(GdkEventKey *)
cairo_pattern_t * led_inset_pattern
std::optional< int > _squaresize
bool on_focus_out_event(GdkEventFocus *)
void on_size_allocate(Gtk::Allocation &)
static Element default_elements
Definition: ardour_button.h:56
ArdourButton(const std::string &, Element e=default_elements, bool toggle=false)
void set_fallthrough_to_parent(bool fall)
Pango::EllipsizeMode _ellipsis
void set_controllable(std::shared_ptr< PBD::Controllable > c)
unsigned int char_pixel_height()
bool is_led_click(GdkEventButton *)
bool on_enter_notify_event(GdkEventCrossing *)
void set_layout_font(const Pango::FontDescription &)
void get_alignment(float &xa, float &ya)
void set_layout_ellipsize_width(int w)
ArdourButton(Element e=default_elements, bool toggle=false)
void set_text(const std::string &, bool markup=false)
std::vector< std::string > _sizing_texts
bool on_key_release_event(GdkEventKey *)
void set_custom_led_color(const uint32_t c, const bool useit=true)
const std::string & get_text() const
Definition: ardour_button.h:99
void set_related_action(Glib::RefPtr< Gtk::Action >)
bool on_button_release_event(GdkEventButton *)
void set_active_color(const uint32_t active_color)
void set_alignment(const float, const float)
sigc::signal< void > signal_clicked
static void set_default_tweaks(Tweaks t)
Definition: ardour_button.h:75
void render(Cairo::RefPtr< Cairo::Context > const &, cairo_rectangle_t *)
void set_fixed_colors(const uint32_t active_color, const uint32_t inactive_color)
cairo_pattern_t * convex_pattern
void set_icon(ArdourIcon::Icon)
void set_text_ellipsize(Pango::EllipsizeMode)
cairo_rectangle_t * _led_rect
bool on_touch_end_event(GdkEventTouch *)
ArdourIcon::Icon icon() const
Definition: ardour_button.h:92
void set_distinct_led_click(bool yn)
std::shared_ptr< PBD::Controllable > get_controllable()
void set_sizing_texts(std::vector< std::string > const &)
static Element just_led_default_elements
Definition: ardour_button.h:58
bool on_leave_notify_event(GdkEventCrossing *)
void set_visual_state(Gtkmm2ext::VisualState)
bool on_button_press_event(GdkEventButton *)
void on_size_request(Gtk::Requisition *req)
#define LIBWIDGETS_API