Zrythm
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
10#ifndef __UTILS_GTK_H__
11#define __UTILS_GTK_H__
12
13#include "zrythm-config.h"
14
15#include <stdbool.h>
16
17#include <gtk/gtk.h>
18#ifdef HAVE_X11
19# include <gdk/x11/gdkx.h>
20#endif
21
22#ifdef _WOE32
23# include <gdk/win32/gdkwin32.h>
24#endif
25
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
28#include <gtksourceview/gtksource.h>
29#pragma GCC diagnostic pop
30
31#include <cyaml/cyaml.h>
32
39#define DEFAULT_CLIPBOARD gdk_display_get_clipboard (gdk_display_get_default ())
40
41#define CREATE_MIDI_LEARN_MENU_ITEM(action) \
42 z_gtk_create_menu_item (_ ("MIDI learn"), "signal-midi", action)
43
44#define CREATE_CUT_MENU_ITEM(action) \
45 z_gtk_create_menu_item (_ ("Cu_t"), "edit-cut", action)
46
47#define CREATE_COPY_MENU_ITEM(action) \
48 z_gtk_create_menu_item (_ ("_Copy"), "edit-copy", action)
49
50#define CREATE_PASTE_MENU_ITEM(action) \
51 z_gtk_create_menu_item (_ ("_Paste"), "edit-paste", action)
52
53#define CREATE_DELETE_MENU_ITEM(action) \
54 z_gtk_create_menu_item (_ ("_Delete"), "edit-delete", action)
55
56#define CREATE_CLEAR_SELECTION_MENU_ITEM(action) \
57 z_gtk_create_menu_item (/* TRANSLATORS: deselects everything */ \
58 _ ("Cle_ar Selection"), "edit-clear", action)
59
60#define CREATE_SELECT_ALL_MENU_ITEM(action) \
61 z_gtk_create_menu_item (_ ("Select A_ll"), "edit-select-all", action)
62
63#define CREATE_DUPLICATE_MENU_ITEM(action) \
64 z_gtk_create_menu_item (_ ("Duplicate"), "edit-duplicate", action)
65
66#define CREATE_MUTE_MENU_ITEM(action) \
67 z_gtk_create_menu_item (_ ("Mute"), "mute", action)
68
69#define CREATE_UNMUTE_MENU_ITEM(action) \
70 z_gtk_create_menu_item (_ ("Unmute"), NULL, action)
71
72#define z_gtk_assistant_set_current_page_complete(assistant, complete) \
73 gtk_assistant_set_page_complete ( \
74 GTK_ASSISTANT (assistant), \
75 gtk_assistant_get_nth_page ( \
76 GTK_ASSISTANT (assistant), \
77 gtk_assistant_get_current_page (GTK_ASSISTANT (assistant))), \
78 complete);
79
80#define Z_GDK_RGBA_INIT(r, g, b, a) \
81 (GdkRGBA) \
82 { \
83 .red = (float) (r), .green = (float) (g), .blue = (float) (b), \
84 .alpha = (float) (a) \
85 }
86
87#define Z_GDK_RECTANGLE_INIT(_x, _y, _w, _h) \
88 (GdkRectangle) \
89 { \
90 .x = _x, .y = _y, .width = _w, .height = _h \
91 }
92
93#define Z_GDK_RECTANGLE_INIT_UNIT(_x, _y) \
94 (GdkRectangle) \
95 { \
96 .x = _x, .y = _y, .width = 1, .height = 1 \
97 }
98
99typedef enum IconType IconType;
100
105typedef struct _ZGObjectImpl
106{
107 GTypeInstance g_type_instance;
108
109 /*< private >*/
110 guint ref_count; /* (atomic) */
111 GData * qdata;
113
114enum ZGtkResize
115{
116 Z_GTK_NO_RESIZE,
117 Z_GTK_RESIZE
118};
119
120enum ZGtkShrink
121{
122 Z_GTK_NO_SHRINK,
123 Z_GTK_SHRINK
124};
125
126static inline GtkWidget *
127z_gtk_notebook_get_current_page_widget (GtkNotebook * notebook)
128{
129 return gtk_notebook_get_nth_page (
130 notebook, gtk_notebook_get_current_page (notebook));
131}
132
133static inline GtkWidget *
134z_gtk_notebook_get_current_tab_label_widget (GtkNotebook * notebook)
135{
136 return gtk_notebook_get_tab_label (
137 notebook, z_gtk_notebook_get_current_page_widget (notebook));
138}
139
140GdkMonitor *
141z_gtk_get_primary_monitor (void);
142
143int
144z_gtk_get_primary_monitor_scale_factor (void);
145
146int
147z_gtk_get_primary_monitor_refresh_rate (void);
148
149bool
150z_gtk_is_wayland (void);
151
152void
153z_gtk_tree_view_remove_all_columns (GtkTreeView * treeview);
154
155void
156z_gtk_column_view_remove_all_columnes (GtkColumnView * column_view);
157
158GListStore *
159z_gtk_column_view_get_list_store (GtkColumnView * column_view);
160
165void
166z_gtk_list_store_splice (GListStore * store, GPtrArray * ptr_array);
167
168void
169z_gtk_widget_remove_all_children (GtkWidget * widget);
170
171void
172z_gtk_widget_destroy_all_children (GtkWidget * widget);
173
174void
175z_gtk_widget_remove_children_of_type (GtkWidget * widget, GType type);
176
177void
178z_gtk_overlay_add_if_not_exists (GtkOverlay * overlay, GtkWidget * widget);
179
183void
185 GtkButton * btn,
186 const char * name,
187 const char * text,
188 bool icon_first,
189 GtkOrientation orientation,
190 int spacing);
191
195GtkToggleButton *
197
201GtkToggleButton *
203 const char * name,
204 const char * text,
205 bool icon_first,
206 GtkOrientation orientation,
207 int spacing);
208
213GtkButton *
215 const char * name,
216 const char * text,
217 bool icon_first,
218 GtkOrientation orientation,
219 int spacing);
220
221#define z_gtk_create_menu_item(lbl_name, icn_name, action_name) \
222 z_gtk_create_menu_item_full (lbl_name, icn_name, action_name)
223
227GMenuItem *
229 const gchar * label_name,
230 const gchar * icon_name,
231 const char * detailed_action);
232
243char *
244z_gtk_get_tooltip_for_action (const char * detailed_action, const char * tooltip);
245
253void
255 GtkWidget * widget,
256 const char * detailed_action,
257 const char * tooltip);
258
263void
265 GtkActionable * actionable,
266 const char * tooltip);
267
268#if 0
272void
273z_gtk_tool_button_set_icon_size (
274 GtkToolButton * toolbutton,
275 GtkIconSize icon_size);
276#endif
277
281static inline GdkDevice *
282z_gtk_widget_get_device (GtkWidget * widget)
283{
284 return (gdk_seat_get_pointer (
285 gdk_display_get_default_seat (gtk_widget_get_display (widget))));
286}
287
288#if 0
289static inline GdkWindow *
290z_gtk_widget_get_root_gdk_window (
291 GtkWidget * widget)
292{
293 GdkScreen * screen =
294 z_gtk_widget_get_screen (widget);
295 return
296 gdk_screen_get_root_window (screen);
297}
298#endif
299
300#if 0
301static inline void
302z_gtk_widget_get_global_coordinates (
303 GtkWidget * widget,
304 int * x,
305 int * y)
306{
307 GdkDevice * dev =
308 z_gtk_widget_get_device (widget);
309 GdkWindow * win =
310 z_gtk_widget_get_root_gdk_window (widget);
311 gdk_window_get_device_position (
312 win, dev, x, y, NULL);
313}
314
315static inline void
316z_gtk_widget_get_global_coordinates_double (
317 GtkWidget * widget,
318 double * x,
319 double * y)
320{
321 GdkDevice * dev =
322 z_gtk_widget_get_device (widget);
323 GdkWindow * win =
324 z_gtk_widget_get_root_gdk_window (widget);
325 gdk_window_get_device_position_double (
326 win, dev, x, y, NULL);
327}
328
332static inline void
333z_gtk_warp_cursor_to (
334 GtkWidget * widget, int x, int y)
335{
336 GdkDevice * dev =
337 z_gtk_widget_get_device (widget);
338 GdkScreen * screen =
339 z_gtk_widget_get_screen (widget);
340 gdk_device_warp (dev, screen, x, y);
341}
342#endif
343
344static inline GdkSurface *
345z_gtk_widget_get_surface (GtkWidget * widget)
346{
347 GtkNative * native = gtk_widget_get_native (widget);
348 return gtk_native_get_surface (native);
349}
350
357static inline void
358z_gtk_widget_get_mask (GtkWidget * widget, GdkModifierType * mask)
359{
360 gdk_surface_get_device_position (
361 z_gtk_widget_get_surface (widget), z_gtk_widget_get_device (widget), NULL,
362 NULL, mask);
363}
364
368static inline int
369z_gtk_keyval_is_alt (const guint keyval)
370{
371 return keyval == GDK_KEY_Alt_L || keyval == GDK_KEY_Alt_R
372 || keyval == GDK_KEY_Meta_L || keyval == GDK_KEY_Meta_R;
373}
374
378static inline int
379z_gtk_keyval_is_ctrl (const guint keyval)
380{
381 return keyval == GDK_KEY_Control_L || keyval == GDK_KEY_Control_R;
382}
383
387static inline int
388z_gtk_keyval_is_arrow (const guint keyval)
389{
390 return keyval == GDK_KEY_Left || keyval == GDK_KEY_Right
391 || keyval == GDK_KEY_Down || keyval == GDK_KEY_Up;
392}
393
397static inline int
398z_gtk_keyval_is_shift (const guint keyval)
399{
400 return keyval == GDK_KEY_Shift_L || keyval == GDK_KEY_Shift_R;
401}
402
403static inline int
404z_gtk_keyval_is_menu (const guint keyval)
405{
406 return keyval == GDK_KEY_Menu;
407}
408
412GtkWidget *
413z_gtk_widget_get_nth_child (GtkWidget * widget, int index);
414
419void
420z_gtk_button_set_emblem (GtkButton * btn, const char * emblem_icon);
421
428void
429z_gtk_setup_foldable_notebook (GtkNotebook * notebook);
430
434void
435z_gtk_widget_set_margin (GtkWidget * widget, int margin);
436
437GtkFlowBoxChild *
438z_gtk_flow_box_get_selected_child (GtkFlowBox * self);
439
443bool
444z_gtk_activate_dir_link_func (GtkLabel * label, char * uri, void * data);
445
446GtkSourceLanguageManager *
447z_gtk_source_language_manager_get (void);
448
453void
455 GtkNotebook * notebook,
456 GtkWindow * parent_window);
457
464char *
465z_gtk_text_buffer_get_full_text (GtkTextBuffer * buffer);
466
480void
482 GtkWidget * widget,
483 const char * type,
484 char ** option_keys,
485 char ** option_values,
486 char ** ret_dir,
487 char ** ret_path,
488 bool accept_fallback);
489
497void
499 GtkActionable * actionable,
500 GSettings * settings,
501 const char * key);
502
503bool
504z_gtk_is_event_button (GdkEvent * ev);
505
510void
512 GtkScrolledWindow * scroll,
513 graphene_rect_t * rect);
514
515void
516z_gtk_graphene_rect_t_to_gdk_rectangle (
517 GdkRectangle * rect,
518 graphene_rect_t * grect);
519
520void
521z_gdk_rectangle_to_graphene_rect_t (graphene_rect_t * grect, GdkRectangle * rect);
522
523void
524z_gdk_rectangle_print (const GdkRectangle * rect);
525
533int
534z_gtk_dialog_run (GtkDialog * dialog, bool destroy_on_close);
535
542void
544 GtkPopoverMenu * popover_menu,
545 double x,
546 double y,
547 GMenu * menu);
548
553GdkDragAction
554z_gtk_drop_target_get_selected_action (GtkDropTarget * drop_target);
555
556GtkIconTheme *
557z_gtk_icon_theme_get_default (void);
558
565char *
566z_gdk_clipboard_get_text (GdkClipboard * clipboard);
567
568#ifdef HAVE_X11
569Window
570z_gtk_window_get_x11_xid (GtkWindow * window);
571#endif
572
573#ifdef _WOE32
574HWND
575z_gtk_window_get_windows_hwnd (GtkWindow * window);
576#endif
577
578#ifdef __APPLE__
579void *
580z_gtk_window_get_nsview (GtkWindow * window);
581#endif
582
590GdkPixbuf *
592 const char * icon_name,
593 int width,
594 int height,
595 int scale,
596 GError ** error);
597
605GdkTexture *
607 const char * icon_name,
608 int width,
609 int height,
610 int scale);
611
612void
613z_gtk_print_graphene_rect (graphene_rect_t * rect);
614
618void
619z_gtk_widget_print_hierarchy (GtkWidget * widget);
620
621const char *
622z_gtk_get_gsk_renderer_type (void);
623
630gboolean
632 GtkWidget * widget,
633 GVariant * args,
634 gpointer user_data);
635
640GtkWidget *
641z_gtk_widget_find_child_of_type (GtkWidget * widget, GType type);
642
643void
644z_gtk_list_box_remove_all_children (GtkListBox * list_box);
645
646void
647z_graphene_rect_print (const graphene_rect_t * rect);
648
649GtkStringList *
650z_gtk_string_list_new_from_cyaml_strvals (
651 const cyaml_strval_t * strvals,
652 size_t num_vals,
653 bool localized);
654
655GtkWidget *
656z_gtk_get_first_focusable_child (GtkWidget * parent);
657
658bool
659z_gtk_descendant_has_focus (GtkWidget * parent);
660
661void
662z_gtk_window_make_escapable (GtkWindow * self);
663
667void
669 GtkSignalListItemFactory * factory,
670 GObject * list_item,
671 gpointer user_data);
672
673void
674z_gtk_drop_down_factory_setup_common (
675 GtkSignalListItemFactory * factory,
676 GObject * list_item,
677 gpointer user_data);
678
679void
680z_gtk_drop_down_factory_setup_common_ellipsized (
681 GtkSignalListItemFactory * factory,
682 GObject * list_item,
683 gpointer user_data);
684
688#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_list_store_splice(GListStore *store, GPtrArray *ptr_array)
Removes all items and re-populates the list store.
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.
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:106