-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk.cpp
1974 lines (1753 loc) · 52 KB
/
gtk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: © 2018-2024 Alexandros Theodotou <[email protected]>
// SPDX-License-Identifier: LicenseRef-ZrythmLicense
/*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* ---
*
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*
* ---
*
* Copyright (c) 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-LicenseIdentifier: LGPL-2.0-or-later
*
* ---
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "gui/widgets/bot_dock_edge.h"
#include "gui/widgets/center_dock.h"
#include "gui/widgets/left_dock_edge.h"
#include "gui/widgets/main_notebook.h"
#include "gui/widgets/main_window.h"
#include "gui/widgets/mixer.h"
#include "gui/widgets/right_dock_edge.h"
#include "settings/settings.h"
#include "utils/gtk.h"
#include "utils/io.h"
#include "utils/objects.h"
#include "utils/resources.h"
#include "utils/string.h"
#include "utils/ui.h"
#include "zrythm.h"
#include "zrythm_app.h"
#ifdef GDK_WINDOWING_WAYLAND
# include <gdk/wayland/gdkwayland.h>
#endif
#include <glib/gi18n.h>
#include <gsk/gskrenderer.h>
typedef enum
{
Z_UTILS_GTK_ERROR_FAILED,
} ZUtilsGtkError;
#define Z_UTILS_GTK_ERROR z_utils_gtk_error_quark ()
GQuark
z_utils_gtk_error_quark (void);
G_DEFINE_QUARK (
z - utils - gtk - error - quark,
z_utils_gtk_error)
GdkMonitor *
z_gtk_get_primary_monitor (void)
{
GdkDisplay * display;
GdkMonitor * monitor;
GListModel * monitor_list;
display = gdk_display_get_default ();
if (!display)
{
g_message ("no display");
return NULL;
}
monitor_list = gdk_display_get_monitors (display);
monitor = (GdkMonitor *) g_list_model_get_item (monitor_list, 0);
if (!monitor)
{
g_message ("no primary monitor");
return NULL;
}
return monitor;
}
int
z_gtk_get_primary_monitor_scale_factor (void)
{
GdkMonitor * monitor;
int scale_factor;
if (ZRYTHM_TESTING || !ZRYTHM_HAVE_UI)
{
return 1;
}
monitor = z_gtk_get_primary_monitor ();
if (!monitor)
{
g_message ("no primary monitor");
goto return_default_scale_factor;
}
scale_factor = gdk_monitor_get_scale_factor (monitor);
if (scale_factor < 1)
{
g_message ("invalid scale factor: %d", scale_factor);
goto return_default_scale_factor;
}
return scale_factor;
return_default_scale_factor:
g_message (
"failed to get refresh rate from device, "
"returning default");
return 1;
}
/**
* Returns the refresh rate in Hz.
*/
int
z_gtk_get_primary_monitor_refresh_rate (void)
{
GdkMonitor * monitor;
int refresh_rate;
if (ZRYTHM_TESTING || !ZRYTHM_HAVE_UI)
{
return 30;
}
monitor = z_gtk_get_primary_monitor ();
if (!monitor)
{
g_warning ("no primary monitor");
goto return_default_refresh_rate;
}
refresh_rate =
/* divide by 1000 because gdk returns the
* value in milli-Hz */
gdk_monitor_get_refresh_rate (monitor) / 1000;
if (refresh_rate == 0)
{
g_warning ("invalid refresh rate: %d", refresh_rate);
goto return_default_refresh_rate;
}
return refresh_rate;
return_default_refresh_rate:
g_warning (
"failed to get refresh rate from device, "
"returning default");
return 30;
}
bool
z_gtk_is_wayland (void)
{
if (ZRYTHM_TESTING || !ZRYTHM_HAVE_UI)
{
return false;
}
GdkDisplay * display = gdk_display_get_default ();
g_return_val_if_fail (display, false);
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY (display))
{
/*g_debug ("wayland");*/
return true;
}
else
{
/*g_debug ("not wayland");*/
}
#endif
return false;
}
void
z_gtk_widget_remove_all_children (GtkWidget * widget)
{
if (GTK_IS_BUTTON (widget))
{
gtk_button_set_child (GTK_BUTTON (widget), NULL);
return;
}
if (GTK_IS_MENU_BUTTON (widget))
{
gtk_menu_button_set_child (GTK_MENU_BUTTON (widget), NULL);
return;
}
if (GTK_IS_LIST_BOX (widget))
{
z_gtk_list_box_remove_all_children (GTK_LIST_BOX (widget));
return;
}
z_gtk_widget_remove_children_of_type (widget, GTK_TYPE_WIDGET);
}
void
z_gtk_overlay_add_if_not_exists (GtkOverlay * overlay, GtkWidget * widget)
{
for (
GtkWidget * child = gtk_widget_get_first_child (GTK_WIDGET (overlay));
child != NULL; child = gtk_widget_get_next_sibling (child))
{
if (child == widget)
{
g_message ("exists");
return;
}
}
g_message ("not exists, adding");
gtk_overlay_add_overlay (overlay, widget);
}
void
z_gtk_widget_destroy_all_children (GtkWidget * widget)
{
/* children are destroyed if there is no extra
* reference when they are removed from their
* parent */
z_gtk_widget_remove_all_children (widget);
}
void
z_gtk_widget_remove_children_of_type (GtkWidget * widget, GType type)
{
for (GtkWidget * child = gtk_widget_get_last_child (widget); child != NULL;)
{
GtkWidget * next_child = gtk_widget_get_prev_sibling (child);
if (G_TYPE_CHECK_INSTANCE_TYPE (child, type))
{
if (GTK_IS_BOX (widget))
{
#if 0
g_debug (
"removing %s (%p) from box %s (%p)",
gtk_widget_get_name (child), child,
gtk_widget_get_name (widget), widget);
#endif
gtk_box_remove (GTK_BOX (widget), child);
}
else
{
#if 0
g_debug (
"unparenting %s (%p) from "
"parent %s (%p)",
gtk_widget_get_name (child), child,
gtk_widget_get_name (widget), widget);
#endif
gtk_widget_unparent (child);
}
}
child = next_child;
}
}
void
z_gtk_tree_view_remove_all_columns (GtkTreeView * treeview)
{
g_return_if_fail (treeview && GTK_IS_TREE_VIEW (treeview));
GtkTreeViewColumn * column;
GList * list, *iter;
list = gtk_tree_view_get_columns (treeview);
for (iter = list; iter != NULL; iter = g_list_next (iter))
{
column = GTK_TREE_VIEW_COLUMN (iter->data);
gtk_tree_view_remove_column (treeview, column);
}
g_list_free (list);
}
void
z_gtk_column_view_remove_all_columnes (GtkColumnView * column_view)
{
g_return_if_fail (column_view && GTK_IS_COLUMN_VIEW (column_view));
GListModel * list = gtk_column_view_get_columns (column_view);
gpointer ptr;
while ((ptr = g_list_model_get_item (list, 0)))
{
GtkColumnViewColumn * col = GTK_COLUMN_VIEW_COLUMN (ptr);
gtk_column_view_remove_column (column_view, col);
}
}
GListStore *
z_gtk_column_view_get_list_store (GtkColumnView * column_view)
{
GtkSelectionModel * sel_model = gtk_column_view_get_model (column_view);
GListModel * model;
if (GTK_IS_MULTI_SELECTION (sel_model))
{
model = gtk_multi_selection_get_model (GTK_MULTI_SELECTION (sel_model));
}
else
{
model = gtk_single_selection_get_model (GTK_SINGLE_SELECTION (sel_model));
}
if (GTK_IS_SORT_LIST_MODEL (model))
{
model = gtk_sort_list_model_get_model (GTK_SORT_LIST_MODEL (model));
}
return G_LIST_STORE (model);
}
/**
* Removes all items and re-populates the list
* store.
*/
void
z_gtk_list_store_splice (GListStore * store, GPtrArray * ptr_array)
{
size_t num_objs;
gpointer * objs = g_ptr_array_steal (ptr_array, &num_objs);
g_list_store_splice (
store, 0, g_list_model_get_n_items (G_LIST_MODEL (store)), objs, num_objs);
g_free (objs);
}
/**
* Sets the icon name and optionally text.
*/
void
z_gtk_button_set_icon_name_and_text (
GtkButton * btn,
const char * name,
const char * text,
bool icon_first,
GtkOrientation orientation,
int spacing)
{
GtkWidget * img = gtk_image_new_from_icon_name (name);
z_gtk_widget_remove_all_children (GTK_WIDGET (btn));
GtkWidget * box = gtk_box_new (orientation, spacing);
GtkWidget * label = gtk_label_new (text);
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
gtk_widget_set_hexpand (label, true);
}
else
{
gtk_widget_set_vexpand (label, true);
}
if (icon_first)
{
gtk_box_append (GTK_BOX (box), img);
gtk_box_append (GTK_BOX (box), label);
}
else
{
gtk_box_append (GTK_BOX (box), label);
gtk_box_append (GTK_BOX (box), img);
}
gtk_button_set_child (GTK_BUTTON (btn), box);
}
/**
* Sets the given emblem to the button, or unsets
* the emblem if \ref emblem_icon is NULL.
*/
void
z_gtk_button_set_emblem (GtkButton * btn, const char * emblem_icon_name)
{
/* TODO */
#if 0
GtkWidget * btn_child =
gtk_bin_get_child (GTK_BIN (btn));
GtkImage * prev_img = NULL;
if (GTK_IS_BIN (btn_child))
{
GtkWidget * inner_child =
gtk_bin_get_child (
GTK_BIN (btn_child));
if (GTK_IS_CONTAINER (inner_child))
{
prev_img =
GTK_IMAGE (
gtk_widget_get_first_child (
GTK_WIDGET (inner_child)));
}
else if (GTK_IS_IMAGE (inner_child))
{
prev_img = GTK_IMAGE (inner_child);
}
else
{
g_critical (
"unknown type %s",
G_OBJECT_TYPE_NAME (inner_child));
}
}
else if (GTK_IS_IMAGE (btn_child))
{
prev_img = GTK_IMAGE (btn_child);
}
else if (GTK_IS_CONTAINER (btn_child))
{
prev_img =
GTK_IMAGE (
gtk_widget_get_first_child (
GTK_WIDGET (btn_child)));
}
else
{
g_return_if_reached ();
}
GtkIconSize icon_size;
const char * icon_name = NULL;
GtkImageType image_type =
gtk_image_get_storage_type (prev_img);
if (image_type == GTK_IMAGE_ICON_NAME)
{
gtk_image_get_icon_name (
prev_img, &icon_name, &icon_size);
}
else if (image_type == GTK_IMAGE_GICON)
{
GIcon * emblemed_icon = NULL;
gtk_image_get_gicon (
prev_img, &emblemed_icon, &icon_size);
g_return_if_fail (emblemed_icon);
GIcon * prev_icon = NULL;
if (G_IS_EMBLEMED_ICON (emblemed_icon))
{
prev_icon =
g_emblemed_icon_get_icon (
G_EMBLEMED_ICON (emblemed_icon));
}
else if (G_IS_THEMED_ICON (emblemed_icon))
{
prev_icon = emblemed_icon;
}
else
{
g_return_if_reached ();
}
const char * const * icon_names =
g_themed_icon_get_names (
G_THEMED_ICON (prev_icon));
icon_name = icon_names[0];
}
else
{
g_return_if_reached ();
}
GIcon * icon = g_themed_icon_new (icon_name);
if (emblem_icon_name)
{
GIcon * dot_icon =
g_themed_icon_new ("media-record");
GEmblem * emblem =
g_emblem_new (dot_icon);
icon =
g_emblemed_icon_new (icon, emblem);
}
/* set new icon */
GtkWidget * img =
gtk_image_new_from_gicon (
icon, icon_size);
gtk_widget_set_visible (img, true);
gtk_button_set_image (btn, img);
#endif
}
/**
* Creates a toggle button with the given icon name.
*/
GtkToggleButton *
z_gtk_toggle_button_new_with_icon (const char * name)
{
GtkToggleButton * btn = GTK_TOGGLE_BUTTON (gtk_toggle_button_new ());
gtk_button_set_icon_name (GTK_BUTTON (btn), name);
return btn;
}
/**
* Creates a toggle button with the given icon name.
*/
GtkToggleButton *
z_gtk_toggle_button_new_with_icon_and_text (
const char * name,
const char * text,
bool icon_first,
GtkOrientation orientation,
int spacing)
{
GtkToggleButton * btn = GTK_TOGGLE_BUTTON (gtk_toggle_button_new ());
z_gtk_button_set_icon_name_and_text (
GTK_BUTTON (btn), name, text, icon_first, orientation, spacing);
return btn;
}
/**
* Creates a button with the given icon name and
* text.
*/
GtkButton *
z_gtk_button_new_with_icon_and_text (
const char * name,
const char * text,
bool icon_first,
GtkOrientation orientation,
int spacing)
{
GtkButton * btn = GTK_BUTTON (gtk_button_new ());
z_gtk_button_set_icon_name_and_text (
GTK_BUTTON (btn), name, text, icon_first, orientation, spacing);
gtk_widget_set_visible (GTK_WIDGET (btn), true);
return btn;
}
/**
* Creates a menu item.
*/
GMenuItem *
z_gtk_create_menu_item_full (
const gchar * label_name,
const gchar * icon_name,
const char * detailed_action)
{
g_return_val_if_fail (label_name, NULL);
GMenuItem * menuitem = g_menu_item_new (label_name, detailed_action);
/* note: setting an icon causes the accelerators
* to not be right-aligned - maybe it's because
* this is an icon name and not a GIcon. FIXME
* pass a GIcon */
#if 0
if (icon_name)
{
g_menu_item_set_attribute (
menuitem, G_MENU_ATTRIBUTE_ICON,
"s", icon_name, NULL);
}
#endif
return menuitem;
}
/**
* Gets the tooltip for the given action on the
* given widget.
*
* If the action is valid, an orange text showing
* the accelerator will be added to the tooltip.
*
* @return A new string that must be free'd with
* g_free().
*/
char *
z_gtk_get_tooltip_for_action (const char * detailed_action, const char * tooltip)
{
char * tmp =
zrythm_app_get_primary_accel_for_action (zrythm_app, detailed_action);
if (tmp)
{
char * accel = g_markup_escape_text (tmp, -1);
g_free (tmp);
char accel_color_hex[90];
ui_gdk_rgba_to_hex (&UI_COLORS->bright_orange, accel_color_hex);
char edited_tooltip[800];
sprintf (
edited_tooltip,
"%s <span size=\"x-small\" "
"foreground=\"%s\">%s</span>",
tooltip, accel_color_hex, accel);
g_free (accel);
return g_strdup (edited_tooltip);
}
else
{
return g_strdup (tooltip);
}
}
/**
* Sets the tooltip for the given action on the
* given widget.
*
* If the action is valid, an orange text showing
* the accelerator will be added to the tooltip.
*/
void
z_gtk_widget_set_tooltip_for_action (
GtkWidget * widget,
const char * detailed_action,
const char * tooltip)
{
char * edited_tooltip =
z_gtk_get_tooltip_for_action (detailed_action, tooltip);
gtk_widget_set_tooltip_markup (widget, edited_tooltip);
g_free (edited_tooltip);
}
/**
* Sets the tooltip and finds the accel keys and
* appends them to the tooltip in small text.
*/
void
z_gtk_set_tooltip_for_actionable (
GtkActionable * actionable,
const char * tooltip)
{
const char * action_name = gtk_actionable_get_action_name (actionable);
char * detailed_action = g_strdup (action_name);
GVariant * target_value = gtk_actionable_get_action_target_value (actionable);
if (target_value)
{
g_free (detailed_action);
detailed_action = g_action_print_detailed_name (action_name, target_value);
}
z_gtk_widget_set_tooltip_for_action (
GTK_WIDGET (actionable), detailed_action, tooltip);
g_free (detailed_action);
}
#if 0
/**
* Changes the size of the icon inside tool buttons.
*/
void
z_gtk_tool_button_set_icon_size (
GtkToolButton * toolbutton,
GtkIconSize icon_size)
{
GtkImage * img =
GTK_IMAGE (
z_gtk_container_get_single_child (
GTK_CONTAINER (
z_gtk_container_get_single_child (
GTK_CONTAINER (
z_gtk_container_get_single_child (
GTK_CONTAINER (toolbutton)))))));
GtkImageType type =
gtk_image_get_storage_type (GTK_IMAGE (img));
g_return_if_fail (type == GTK_IMAGE_ICON_NAME);
const char * _icon_name;
gtk_image_get_icon_name (
GTK_IMAGE (img), &_icon_name, NULL);
char * icon_name = g_strdup (_icon_name);
gtk_image_set_from_icon_name (
GTK_IMAGE (img), icon_name,
GTK_ICON_SIZE_SMALL_TOOLBAR);
g_free (icon_name);
}
#endif
/**
* Returns the nth child of a container.
*/
GtkWidget *
z_gtk_widget_get_nth_child (GtkWidget * widget, int index)
{
int i = 0;
for (
GtkWidget * child = gtk_widget_get_first_child (widget); child != NULL;
child = gtk_widget_get_next_sibling (child))
{
if (i != index)
{
i++;
continue;
}
return child;
}
g_return_val_if_reached (NULL);
}
/**
* Sets the margin on all 4 sides on the widget.
*/
void
z_gtk_widget_set_margin (GtkWidget * widget, int margin)
{
gtk_widget_set_margin_start (widget, margin);
gtk_widget_set_margin_end (widget, margin);
gtk_widget_set_margin_top (widget, margin);
gtk_widget_set_margin_bottom (widget, margin);
}
GtkFlowBoxChild *
z_gtk_flow_box_get_selected_child (GtkFlowBox * self)
{
GList * list = gtk_flow_box_get_selected_children (self);
GtkFlowBoxChild * sel_child = NULL;
for (GList * l = list; l != NULL; l = l->next)
{
sel_child = (GtkFlowBoxChild *) l->data;
break;
}
g_list_free (list);
return sel_child;
}
/**
* Callback to use for simple directory links.
*/
bool
z_gtk_activate_dir_link_func (GtkLabel * label, char * uri, void * data)
{
io_open_directory (uri);
return TRUE;
}
GtkSourceLanguageManager *
z_gtk_source_language_manager_get (void)
{
GtkSourceLanguageManager * manager =
gtk_source_language_manager_get_default ();
static bool already_set = false;
if (already_set)
{
return manager;
}
/* get the default search paths */
const char * const * before_paths =
gtk_source_language_manager_get_search_path (manager);
/* build the new paths */
GStrvBuilder * after_paths_builder = g_strv_builder_new ();
GStrvBuilder * after_paths_builder_tmp = g_strv_builder_new ();
int i = 0;
while (before_paths[i])
{
g_debug ("language specs dir %d: %s", i, before_paths[i]);
g_strv_builder_add (after_paths_builder, before_paths[i]);
g_strv_builder_add (after_paths_builder_tmp, before_paths[i]);
i++;
}
/* add the new path if not already in the list */
char * language_specs_dir =
gZrythm->dir_mgr->get_dir (ZRYTHM_DIR_SYSTEM_SOURCEVIEW_LANGUAGE_SPECS_DIR);
g_return_val_if_fail (language_specs_dir, NULL);
char ** tmp_dirs = g_strv_builder_end (after_paths_builder_tmp);
if (!g_strv_contains ((const char * const *) tmp_dirs, language_specs_dir))
{
g_strv_builder_add (after_paths_builder, language_specs_dir);
}
g_strfreev (tmp_dirs);
g_free (language_specs_dir);
/* add bundled dir for GNU/Linux packages */
language_specs_dir = gZrythm->dir_mgr->get_dir (
ZRYTHM_DIR_SYSTEM_BUNDLED_SOURCEVIEW_LANGUAGE_SPECS_DIR);
g_strv_builder_add (after_paths_builder, language_specs_dir);
g_free (language_specs_dir);
char ** dirs = g_strv_builder_end (after_paths_builder);
i = 0;
while (dirs[i])
{
const char * dir = dirs[i];
g_message ("%d: %s", i, dir);
i++;
}
gtk_source_language_manager_set_search_path (
manager, (const char * const *) dirs);
g_strfreev (dirs);
already_set = true;
#if 0
/* print found language specs */
const char * const * lang_ids =
gtk_source_language_manager_get_language_ids (
manager);
const char * lang_id = NULL;
i = 0;
while ((lang_id = lang_ids[i++]) != NULL)
{
g_debug ("[%d] %s", i, lang_id);
}
#endif
return manager;
}
typedef struct DetachableNotebookData
{
/** Parent window of original notebook. */
GtkWindow * parent_window;
/** Original notebook. */
GtkNotebook * notebook;
/** Windows for dropping detached tabs. */
GPtrArray * new_windows;
/** New notebooks inside new windows. */
GPtrArray * new_notebooks;
/** Hashtable of settings schema keys => widget
* pointers. */
GHashTable * ht;
/** Window title. */
const char * title;
/** Window role. */
const char * role;
} DetachableNotebookData;
static void
on_new_notebook_page_removed (
GtkNotebook * notebook,
GtkWidget * child,
guint page_num,
GtkWindow * new_window)
{
/* destroy the sub window after the notebook is
* empty */
if (gtk_notebook_get_n_pages (notebook) == 0)
{
gtk_window_destroy (new_window);
}
}
static void
on_new_window_destroyed (GtkWidget * widget, DetachableNotebookData * data)
{
guint idx;
bool found = g_ptr_array_find (data->new_windows, widget, &idx);
g_return_if_fail (found);
GtkNotebook * new_notebook =
(GtkNotebook *) g_ptr_array_index (data->new_notebooks, idx);
const char * name = gtk_widget_get_name (GTK_WIDGET (new_notebook));
g_debug ("widget %s (%p)", name, new_notebook);
g_return_if_fail (GTK_IS_NOTEBOOK (new_notebook));
g_ptr_array_remove_index (data->new_windows, idx);
g_ptr_array_remove_index (data->new_notebooks, idx);
/* if the sub window gets destroyed, push pages
* back to the main window */
/* detach the notebook pages in reverse sequence
* to avoid index errors */
int n_pages = gtk_notebook_get_n_pages (new_notebook);
for (int i = n_pages - 1; i >= 0; i--)
{
GtkWidget * page = gtk_notebook_get_nth_page (new_notebook, i);
GtkWidget * tab_label = gtk_notebook_get_tab_label (new_notebook, page);
g_object_ref (page);
g_object_ref (tab_label);
gtk_notebook_detach_tab (new_notebook, page);
gtk_notebook_append_page (data->notebook, page, tab_label);
g_object_unref (page);
g_object_unref (tab_label);
gtk_notebook_set_tab_detachable (data->notebook, page, true);
gtk_notebook_set_tab_reorderable (data->notebook, page, true);
}
g_object_unref (new_notebook);
}
typedef struct DetachableNotebookDeleteEventData
{
DetachableNotebookData * data;
GtkWidget * page;
} DetachableNotebookDeleteEventData;
static bool
on_new_window_close_request (
GtkWidget * widget,
DetachableNotebookDeleteEventData * delete_data)
{
GtkWidget * page = delete_data->page;
char * val = (char *) g_hash_table_lookup (delete_data->data->ht, page);
g_return_val_if_fail (val, false);
char key_detached[600];
sprintf (key_detached, "%s-detached", val);
char key_size[600];
sprintf (key_size, "%s-size", val);
int w, h;
gtk_window_get_default_size (GTK_WINDOW (widget), &w, &h);
g_settings_set_boolean (S_UI_PANELS, key_detached, false);
g_settings_set (S_UI_PANELS, key_size, "(ii)", w, h);
g_debug ("saving %s size %d %d", val, w, w);
return false;
}
static void
free_delete_data (DetachableNotebookDeleteEventData * data, GClosure * closure)
{
free (data);
}
static GtkNotebook *
on_create_window (
GtkNotebook * notebook,
GtkWidget * page,
DetachableNotebookData * data)
{
g_return_val_if_fail (data, NULL);
GtkWindow * new_window = GTK_WINDOW (gtk_window_new ());
GtkNotebook * new_notebook = GTK_NOTEBOOK (gtk_notebook_new ());
g_object_ref_sink (new_notebook);
gtk_window_set_child (GTK_WINDOW (new_window), GTK_WIDGET (new_notebook));
const char * title = "Zrythm";
/*const char * role = "zrythm-panel";*/
#define SET_TITLE_AND_ROLE(w, t, r) \
if (page == GTK_WIDGET (w)) \
{ \
title = t; \
}
/* FIXME the names should be fetched from the
* tab labels instead of repeating the names
* here */
SET_TITLE_AND_ROLE (MW_BOT_DOCK_EDGE->mixer_box, _ ("Mixer"), "mixer");
SET_TITLE_AND_ROLE (
MW_BOT_DOCK_EDGE->modulator_view_box, _ ("Modulators"), "modulators");
SET_TITLE_AND_ROLE (
MW_BOT_DOCK_EDGE->chord_pad_panel_box, _ ("Chord Pad"), "chord-pad");
SET_TITLE_AND_ROLE (MW_BOT_DOCK_EDGE->clip_editor_box, _ ("Editor"), "editor");
SET_TITLE_AND_ROLE (
MW_LEFT_DOCK_EDGE->track_inspector_scroll, _ ("Track Inspector"),
"track-inspector");
SET_TITLE_AND_ROLE (
MW_LEFT_DOCK_EDGE->plugin_inspector_scroll, _ ("Plugin Inspector"),
"plugin-inspector");
SET_TITLE_AND_ROLE (
MW_RIGHT_DOCK_EDGE->plugin_browser_box, _ ("Plugin Browser"),
"plugin-browser");
SET_TITLE_AND_ROLE (
MW_RIGHT_DOCK_EDGE->file_browser_box, _ ("File Browser"), "file-browser");
SET_TITLE_AND_ROLE (
MW_RIGHT_DOCK_EDGE->chord_pack_browser_box, _ ("Chord Preset Browser"),
"chord-pack-browser");
SET_TITLE_AND_ROLE (
MW_RIGHT_DOCK_EDGE->monitor_section_box, _ ("Monitor"), "monitor");
SET_TITLE_AND_ROLE (
MW_MAIN_NOTEBOOK->timeline_plus_event_viewer_paned, _ ("Timeline"),
"timeline");
SET_TITLE_AND_ROLE (
MW_MAIN_NOTEBOOK->cc_bindings_box, _ ("MIDI CC Bindings"),
"midi-cc-bindings");