1 /*
2 * Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "audio/snap_grid.h"
21 #include "gui/widgets/snap_grid.h"
22 #include "gui/widgets/snap_grid_popover.h"
23 #include "utils/flags.h"
24 #include "utils/gtk.h"
25 #include "utils/resources.h"
26
27 #include <gtk/gtk.h>
28
29 #include <glib/gi18n.h>
30
G_DEFINE_TYPE(SnapGridWidget,snap_grid_widget,GTK_TYPE_MENU_BUTTON)31 G_DEFINE_TYPE (
32 SnapGridWidget, snap_grid_widget,
33 GTK_TYPE_MENU_BUTTON)
34
35 static void
36 on_clicked (GtkButton * button,
37 SnapGridWidget * self)
38 {
39 /*gtk_widget_show_all (GTK_WIDGET (self->popover));*/
40 }
41
42 static void
set_label(SnapGridWidget * self)43 set_label (SnapGridWidget * self)
44 {
45 SnapGrid * sg = self->snap_grid;
46 char * snap_str =
47 snap_grid_stringize (
48 sg->snap_note_length,
49 sg->snap_note_type);
50
51 char new_str[600];
52 if (sg->length_type == NOTE_LENGTH_LINK)
53 {
54 sprintf (new_str, "%s - ", snap_str);
55 }
56 else if (sg->length_type ==
57 NOTE_LENGTH_LAST_OBJECT)
58 {
59 sprintf (
60 new_str, _("%s - Last object"), snap_str);
61 }
62 else
63 {
64 char * default_str =
65 snap_grid_stringize (
66 sg->default_note_length,
67 sg->default_note_type);
68 sprintf (
69 new_str, "%s - %s", snap_str, default_str);
70 g_free (default_str);
71 }
72 gtk_label_set_text (self->label, new_str);
73
74 g_free (snap_str);
75 }
76
77 void
snap_grid_widget_refresh(SnapGridWidget * self)78 snap_grid_widget_refresh (
79 SnapGridWidget * self)
80 {
81 set_label (self);
82 }
83
84 void
snap_grid_widget_setup(SnapGridWidget * self,SnapGrid * snap_grid)85 snap_grid_widget_setup (
86 SnapGridWidget * self,
87 SnapGrid * snap_grid)
88 {
89 self->snap_grid = snap_grid;
90 self->popover =
91 snap_grid_popover_widget_new (self);
92 gtk_menu_button_set_popover (
93 GTK_MENU_BUTTON (self),
94 GTK_WIDGET (self->popover));
95
96 set_label (self);
97 }
98
99 static void
snap_grid_widget_class_init(SnapGridWidgetClass * klass)100 snap_grid_widget_class_init (
101 SnapGridWidgetClass * klass)
102 {
103 }
104
105 static void
snap_grid_widget_init(SnapGridWidget * self)106 snap_grid_widget_init (SnapGridWidget * self)
107 {
108 self->box =
109 GTK_BOX (
110 gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
111 self->img =
112 GTK_IMAGE (
113 gtk_image_new_from_icon_name (
114 "snap-to-grid",
115 GTK_ICON_SIZE_SMALL_TOOLBAR));
116 self->label =
117 GTK_LABEL (gtk_label_new (""));
118 gtk_widget_set_tooltip_text (
119 GTK_WIDGET (self->box),
120 _("Snap/Grid options"));
121 gtk_box_pack_start (
122 self->box, GTK_WIDGET (self->img),
123 F_NO_EXPAND, F_NO_FILL, 1);
124 gtk_box_pack_end (
125 self->box, GTK_WIDGET (self->label),
126 F_NO_EXPAND, F_NO_FILL, 1);
127 gtk_container_add (
128 GTK_CONTAINER (self), GTK_WIDGET (self->box));
129 g_signal_connect (
130 G_OBJECT (self), "clicked",
131 G_CALLBACK (on_clicked), self);
132
133 gtk_widget_show_all (GTK_WIDGET (self));
134 }
135
136