Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
gtk.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2018-2024 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
9
10#ifndef __UTILS_GTK_H__
11#define __UTILS_GTK_H__
12
13#include "zrythm-config.h"
14
15#include <vector>
16
17#include "gui/backend/gtk_widgets/gtk_wrapper.h"
18
19class Color;
20
26
27#define DEFAULT_CLIPBOARD gdk_display_get_clipboard (gdk_display_get_default ())
28
29#define CREATE_MIDI_LEARN_MENU_ITEM(action) \
30 z_gtk_create_menu_item (_ ("MIDI learn"), "signal-midi", action)
31
32#define CREATE_CUT_MENU_ITEM(action) \
33 z_gtk_create_menu_item (_ ("Cu_t"), "edit-cut", action)
34
35#define CREATE_COPY_MENU_ITEM(action) \
36 z_gtk_create_menu_item (_ ("_Copy"), "edit-copy", action)
37
38#define CREATE_PASTE_MENU_ITEM(action) \
39 z_gtk_create_menu_item (_ ("_Paste"), "edit-paste", action)
40
41#define CREATE_DELETE_MENU_ITEM(action) \
42 z_gtk_create_menu_item (_ ("_Delete"), "edit-delete", action)
43
44#define CREATE_CLEAR_SELECTION_MENU_ITEM(action) \
45 z_gtk_create_menu_item (/* TRANSLATORS: deselects everything */ \
46 _ ("Cle_ar Selection"), "edit-clear", action)
47
48#define CREATE_SELECT_ALL_MENU_ITEM(action) \
49 z_gtk_create_menu_item (_ ("Select A_ll"), "edit-select-all", action)
50
51#define CREATE_DUPLICATE_MENU_ITEM(action) \
52 z_gtk_create_menu_item (_ ("Duplicate"), "edit-duplicate", action)
53
54#define CREATE_MUTE_MENU_ITEM(action) \
55 z_gtk_create_menu_item (_ ("Mute"), "mute", action)
56
57#define CREATE_UNMUTE_MENU_ITEM(action) \
58 z_gtk_create_menu_item (_ ("Unmute"), nullptr, action)
59
60#define z_gtk_assistant_set_current_page_complete(assistant, complete) \
61 gtk_assistant_set_page_complete ( \
62 GTK_ASSISTANT (assistant), \
63 gtk_assistant_get_nth_page ( \
64 GTK_ASSISTANT (assistant), \
65 gtk_assistant_get_current_page (GTK_ASSISTANT (assistant))), \
66 complete);
67
68#define Z_GDK_RGBA_INIT(r, g, b, a) \
69 { .red = static_cast<float> (r), \
70 .green = static_cast<float> (g), \
71 .blue = static_cast<float> (b), \
72 .alpha = static_cast<float> (a) }
73
74#define Z_GDK_RECTANGLE_INIT(_x, _y, _w, _h) \
75 { .x = _x, .y = _y, .width = _w, .height = _h }
76
77#define Z_GDK_RECTANGLE_INIT_UNIT(_x, _y) \
78 { .x = _x, .y = _y, .width = 1, .height = 1 }
79
80#define Z_GRAPHENE_RECT_INIT(_x, _y, _w, _h) \
81 { \
82 .origin = { .x = (_x), .y = (_y) }, .size = { \
83 .width = (_w), \
84 .height = (_h) \
85 } \
86 }
87
88#define Z_GRAPHENE_POINT_INIT(_x, _y) \
89 { .x = static_cast<float> (_x), .y = static_cast<float> (_y) }
90
91#define Z_GRAPHENE_SIZE_INIT(_w, _h) \
92 { .width = static_cast<float> (_w), .height = static_cast<float> (_h) }
93
98typedef struct _ZGObjectImpl
99{
100 GTypeInstance g_type_instance;
101
102 /*< private >*/
103 guint ref_count; /* (atomic) */
104 GData * qdata;
106
107enum ZGtkResize
108{
109 Z_GTK_NO_RESIZE,
110 Z_GTK_RESIZE
111};
112
113enum ZGtkShrink
114{
115 Z_GTK_NO_SHRINK,
116 Z_GTK_SHRINK
117};
118
119static inline GtkWidget *
120z_gtk_notebook_get_current_page_widget (GtkNotebook * notebook)
121{
122 return gtk_notebook_get_nth_page (
123 notebook, gtk_notebook_get_current_page (notebook));
124}
125
126static inline GtkWidget *
127z_gtk_notebook_get_current_tab_label_widget (GtkNotebook * notebook)
128{
129 return gtk_notebook_get_tab_label (
130 notebook, z_gtk_notebook_get_current_page_widget (notebook));
131}
132
133GdkMonitor *
134z_gtk_get_primary_monitor (void);
135
136int
137z_gtk_get_primary_monitor_scale_factor (void);
138
139int
140z_gtk_get_primary_monitor_refresh_rate (void);
141
142bool
143z_gtk_is_wayland (void);
144
145void
146z_gtk_tree_view_remove_all_columns (GtkTreeView * treeview);
147
148void
149z_gtk_column_view_remove_all_columns (GtkColumnView * column_view);
150
151GListStore *
152z_gtk_column_view_get_list_store (GtkColumnView * column_view);
153
157void
158z_gtk_list_store_splice (GListStore * store, auto &items)
159{
160 static_assert (
161 std::is_pointer_v<typename std::decay_t<decltype (items)>::value_type>,
162 "items must be a vector of pointers");
163 std::vector<gpointer> additions (items.begin (), items.end ());
164 g_list_store_splice (
165 store, 0, g_list_model_get_n_items (G_LIST_MODEL (store)),
166 additions.data (), additions.size ());
167}
168
169void
170z_gtk_widget_remove_all_children (GtkWidget * widget);
171
172void
173z_gtk_widget_destroy_all_children (GtkWidget * widget);
174
175void
176z_gtk_widget_remove_children_of_type (GtkWidget * widget, GType type);
177
178void
179z_gtk_overlay_add_if_not_exists (GtkOverlay * overlay, GtkWidget * widget);
180
184void
186 GtkButton * btn,
187 const char * name,
188 const char * text,
189 bool icon_first,
190 GtkOrientation orientation,
191 int spacing);
192
196GtkToggleButton *
198
202GtkToggleButton *
204 const char * name,
205 const char * text,
206 bool icon_first,
207 GtkOrientation orientation,
208 int spacing);
209
214GtkButton *
216 const char * name,
217 const char * text,
218 bool icon_first,
219 GtkOrientation orientation,
220 int spacing);
221
222#define z_gtk_create_menu_item(lbl_name, icn_name, action_name) \
223 z_gtk_create_menu_item_full (lbl_name, icn_name, action_name)
224
228GMenuItem *
230 const gchar * label_name,
231 const gchar * icon_name,
232 const char * detailed_action);
233
244char *
245z_gtk_get_tooltip_for_action (const char * detailed_action, const char * tooltip);
246
254void
256 GtkWidget * widget,
257 const char * detailed_action,
258 const char * tooltip);
259
264void
266 GtkActionable * actionable,
267 const char * tooltip);
268
269#if 0
273void
274z_gtk_tool_button_set_icon_size (
275 GtkToolButton * toolbutton,
276 GtkIconSize icon_size);
277#endif
278
282static inline GdkDevice *
283z_gtk_widget_get_device (GtkWidget * widget)
284{
285 return (gdk_seat_get_pointer (
286 gdk_display_get_default_seat (gtk_widget_get_display (widget))));
287}
288
289#if 0
290static inline GdkWindow *
291z_gtk_widget_get_root_gdk_window (
292 GtkWidget * widget)
293{
294 GdkScreen * screen =
295 z_gtk_widget_get_screen (widget);
296 return
297 gdk_screen_get_root_window (screen);
298}
299#endif
300
301#if 0
302static inline void
303z_gtk_widget_get_global_coordinates (
304 GtkWidget * widget,
305 int * x,
306 int * y)
307{
308 GdkDevice * dev =
309 z_gtk_widget_get_device (widget);
310 GdkWindow * win =
311 z_gtk_widget_get_root_gdk_window (widget);
312 gdk_window_get_device_position (
313 win, dev, x, y, nullptr);
314}
315
316static inline void
317z_gtk_widget_get_global_coordinates_double (
318 GtkWidget * widget,
319 double * x,
320 double * y)
321{
322 GdkDevice * dev =
323 z_gtk_widget_get_device (widget);
324 GdkWindow * win =
325 z_gtk_widget_get_root_gdk_window (widget);
326 gdk_window_get_device_position_double (
327 win, dev, x, y, nullptr);
328}
329
333static inline void
334z_gtk_warp_cursor_to (
335 GtkWidget * widget, int x, int y)
336{
337 GdkDevice * dev =
338 z_gtk_widget_get_device (widget);
339 GdkScreen * screen =
340 z_gtk_widget_get_screen (widget);
341 gdk_device_warp (dev, screen, x, y);
342}
343#endif
344
345static inline GdkSurface *
346z_gtk_widget_get_surface (GtkWidget * widget)
347{
348 GtkNative * native = gtk_widget_get_native (widget);
349 return gtk_native_get_surface (native);
350}
351
358static inline void
359z_gtk_widget_get_mask (GtkWidget * widget, GdkModifierType * mask)
360{
361 gdk_surface_get_device_position (
362 z_gtk_widget_get_surface (widget), z_gtk_widget_get_device (widget),
363 nullptr, nullptr, mask);
364}
365
369static inline int
370z_gtk_keyval_is_alt (const guint keyval)
371{
372 return keyval == GDK_KEY_Alt_L || keyval == GDK_KEY_Alt_R
373 || keyval == GDK_KEY_Meta_L || keyval == GDK_KEY_Meta_R;
374}
375
379static inline int
380z_gtk_keyval_is_ctrl (const guint keyval)
381{
382 return keyval == GDK_KEY_Control_L || keyval == GDK_KEY_Control_R;
383}
384
388static inline int
389z_gtk_keyval_is_arrow (const guint keyval)
390{
391 return keyval == GDK_KEY_Left || keyval == GDK_KEY_Right
392 || keyval == GDK_KEY_Down || keyval == GDK_KEY_Up;
393}
394
398static inline int
399z_gtk_keyval_is_shift (const guint keyval)
400{
401 return keyval == GDK_KEY_Shift_L || keyval == GDK_KEY_Shift_R;
402}
403
404static inline int
405z_gtk_keyval_is_menu (const guint keyval)
406{
407 return keyval == GDK_KEY_Menu;
408}
409
413GtkWidget *
414z_gtk_widget_get_nth_child (GtkWidget * widget, int index);
415
420void
421z_gtk_button_set_emblem (GtkButton * btn, const char * emblem_icon);
422
429void
430z_gtk_setup_foldable_notebook (GtkNotebook * notebook);
431
435void
436z_gtk_widget_set_margin (GtkWidget * widget, int margin);
437
438GtkFlowBoxChild *
439z_gtk_flow_box_get_selected_child (GtkFlowBox * self);
440
444bool
445z_gtk_activate_dir_link_func (GtkLabel * label, char * uri, void * data);
446
447#if 0
448GtkSourceLanguageManager *
449z_gtk_source_language_manager_get (void);
450#endif
451
456void
458 GtkNotebook * notebook,
459 GtkWindow * parent_window);
460
467char *
468z_gtk_text_buffer_get_full_text (GtkTextBuffer * buffer);
469
483void
485 GtkWidget * widget,
486 const char * type,
487 char ** option_keys,
488 char ** option_values,
489 char ** ret_dir,
490 char ** ret_path,
491 bool accept_fallback);
492
500void
502 GtkActionable * actionable,
503 GSettings * settings,
504 const char * key);
505
506bool
507z_gtk_is_event_button (GdkEvent * ev);
508
513void
515 GtkScrolledWindow * scroll,
516 graphene_rect_t * rect);
517
518void
519z_gtk_graphene_rect_t_to_gdk_rectangle (
520 GdkRectangle * rect,
521 graphene_rect_t * grect);
522
523void
524z_gdk_rectangle_to_graphene_rect_t (graphene_rect_t * grect, GdkRectangle * rect);
525
526void
527z_gdk_rectangle_print (const GdkRectangle * rect);
528
536int
537z_gtk_dialog_run (GtkDialog * dialog, bool destroy_on_close);
538
545void
547 GtkPopoverMenu * popover_menu,
548 double x,
549 double y,
550 GMenu * menu);
551
556GdkDragAction
557z_gtk_drop_target_get_selected_action (GtkDropTarget * drop_target);
558
559GtkIconTheme *
560z_gtk_icon_theme_get_default (void);
561
568char *
569z_gdk_clipboard_get_text (GdkClipboard * clipboard);
570
571#if HAVE_X11
572Window
573z_gtk_window_get_x11_xid (GtkWindow * window);
574#endif
575
576#ifdef _WIN32
577HWND
578z_gtk_window_get_windows_hwnd (GtkWindow * window);
579#endif
580
581#ifdef __APPLE__
582void *
583z_gtk_window_get_nsview (GtkWindow * window);
584#endif
585
593GdkPixbuf *
595 const char * icon_name,
596 int width,
597 int height,
598 int scale,
599 GError ** error);
600
608GdkTexture *
610 const char * icon_name,
611 int width,
612 int height,
613 int scale);
614
615void
616z_gtk_print_graphene_rect (graphene_rect_t * rect);
617
621void
622z_gtk_widget_print_hierarchy (GtkWidget * widget);
623
624const char *
625z_gtk_get_gsk_renderer_type (void);
626
633gboolean
635 GtkWidget * widget,
636 GVariant * args,
637 gpointer user_data);
638
643GtkWidget *
644z_gtk_widget_find_child_of_type (GtkWidget * widget, GType type);
645
646void
647z_gtk_list_box_remove_all_children (GtkListBox * list_box);
648
649void
650z_graphene_rect_print (const graphene_rect_t * rect);
651
652GtkStringList *
653z_gtk_string_list_new_from_string_array (
654 const char ** strvals,
655 size_t num_vals,
656 bool localized);
657
658GtkWidget *
659z_gtk_get_first_focusable_child (GtkWidget * parent);
660
661bool
662z_gtk_descendant_has_focus (GtkWidget * parent);
663
664void
665z_gtk_window_make_escapable (GtkWindow * self);
666
670void
672 GtkSignalListItemFactory * factory,
673 GObject * list_item,
674 gpointer user_data);
675
676void
677z_gtk_drop_down_factory_setup_common (
678 GtkSignalListItemFactory * factory,
679 GObject * list_item,
680 gpointer user_data);
681
682void
683z_gtk_drop_down_factory_setup_common_ellipsized (
684 GtkSignalListItemFactory * factory,
685 GObject * list_item,
686 gpointer user_data);
687
688const char *
689z_gtk_get_enum_nick (GType type, gint value);
690
691void
692z_gtk_snapshot_append_color (
693 GtkSnapshot * snapshot,
694 Color color,
695 const graphene_rect_t * rect);
696
700#endif
void z_gtk_setup_foldable_notebook(GtkNotebook *notebook)
Makes the given notebook foldable.
void z_gtk_generate_screenshot_image(GtkWidget *widget, const char *type, char **option_keys, char **option_values, char **ret_dir, char **ret_path, bool accept_fallback)
Generates a screenshot image for the given widget.
void z_gtk_notebook_make_detachable(GtkNotebook *notebook, GtkWindow *parent_window)
Makes the given GtkNotebook detachable to a new window.
bool z_gtk_activate_dir_link_func(GtkLabel *label, char *uri, void *data)
Callback to use for simple directory links.
int z_gtk_dialog_run(GtkDialog *dialog, bool destroy_on_close)
Mimics the blocking behavior.
char * z_gdk_clipboard_get_text(GdkClipboard *clipboard)
Returns the text on the clipboard, or NULL if there is nothing or the content is not text.
void z_gtk_button_set_emblem(GtkButton *btn, const char *emblem_icon)
Sets the given emblem to the button, or unsets the emblem if emblem_icon is NULL.
GtkToggleButton * z_gtk_toggle_button_new_with_icon(const char *name)
Creates a toggle button with the given icon name.
void z_gtk_actionable_set_action_from_setting(GtkActionable *actionable, GSettings *settings, const char *key)
Sets the action target of the given GtkActionable to be binded to the given setting.
void z_gtk_show_context_menu_from_g_menu(GtkPopoverMenu *popover_menu, double x, double y, GMenu *menu)
The popover must already exist as a children of its intended widget (or a common parent).
GtkButton * z_gtk_button_new_with_icon_and_text(const char *name, const char *text, bool icon_first, GtkOrientation orientation, int spacing)
Creates a button with the given icon name and text.
void z_gtk_widget_set_tooltip_for_action(GtkWidget *widget, const char *detailed_action, const char *tooltip)
Sets the tooltip for the given action on the given widget.
void z_gtk_widget_set_margin(GtkWidget *widget, int margin)
Sets the margin on all 4 sides on the widget.
GtkWidget * z_gtk_widget_find_child_of_type(GtkWidget *widget, GType type)
Recursively searches the children of widget for a child of type type.
gboolean z_gtk_simple_action_shortcut_func(GtkWidget *widget, GVariant *args, gpointer user_data)
A shortcut callback to use for simple actions.
GdkTexture * z_gdk_texture_new_from_icon_name(const char *icon_name, int width, int height, int scale)
Creates a new texture for the given icon scaled at the given width/height.
GdkDragAction z_gtk_drop_target_get_selected_action(GtkDropTarget *drop_target)
Returns the bitmask of the selected action during a drop (eg, GDK_ACTION_COPY).
char * z_gtk_get_tooltip_for_action(const char *detailed_action, const char *tooltip)
Gets the tooltip for the given action on the given widget.
GdkPixbuf * z_gdk_pixbuf_new_from_icon_name(const char *icon_name, int width, int height, int scale, GError **error)
Creates a new pixbuf for the given icon scaled at the given width/height.
void z_gtk_drop_down_list_item_header_setup_common(GtkSignalListItemFactory *factory, GObject *list_item, gpointer user_data)
A common "setup" signal handler for GtkDropDown header factories.
GMenuItem * z_gtk_create_menu_item_full(const gchar *label_name, const gchar *icon_name, const char *detailed_action)
Creates a menu item.
void z_gtk_scrolled_window_get_visible_rect(GtkScrolledWindow *scroll, graphene_rect_t *rect)
Gets the visible rectangle from the scrolled window's adjustments.
void z_gtk_list_store_splice(GListStore *store, auto &items)
Removes all items and re-populates the list store.
Definition gtk.h:158
GtkWidget * z_gtk_widget_get_nth_child(GtkWidget *widget, int index)
Returns the nth child of a container.
void z_gtk_button_set_icon_name_and_text(GtkButton *btn, const char *name, const char *text, bool icon_first, GtkOrientation orientation, int spacing)
Sets the icon name and optionally text.
void z_gtk_widget_print_hierarchy(GtkWidget *widget)
Prints the widget's hierarchy (parents).
void z_gtk_set_tooltip_for_actionable(GtkActionable *actionable, const char *tooltip)
Sets the tooltip and finds the accel keys and appends them to the tooltip in small text.
GtkToggleButton * z_gtk_toggle_button_new_with_icon_and_text(const char *name, const char *text, bool icon_first, GtkOrientation orientation, int spacing)
Creates a toggle button with the given icon name.
char * z_gtk_text_buffer_get_full_text(GtkTextBuffer *buffer)
Returns the full text contained in the text buffer.
GObject struct (from GObject source code), used where hacks are needed.
Definition gtk.h:99