1 /*
2 * Copyright (C) 2019 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/chord_descriptor.h"
21 #include "audio/chord_object.h"
22 #include "audio/chord_track.h"
23 #include "audio/midi.h"
24 #include "gui/backend/clip_editor.h"
25 #include "gui/widgets/bot_dock_edge.h"
26 #include "gui/widgets/center_dock.h"
27 #include "gui/widgets/chord_editor_space.h"
28 #include "gui/widgets/chord_key.h"
29 #include "gui/widgets/chord_selector_window.h"
30 #include "gui/widgets/clip_editor.h"
31 #include "gui/widgets/clip_editor_inner.h"
32 #include "gui/widgets/main_window.h"
33 #include "gui/widgets/piano_keyboard.h"
34 #include "project.h"
35 #include "utils/cairo.h"
36 #include "utils/gtk.h"
37 #include "utils/resources.h"
38
39 #include <gtk/gtk.h>
40
G_DEFINE_TYPE(ChordKeyWidget,chord_key_widget,GTK_TYPE_GRID)41 G_DEFINE_TYPE (ChordKeyWidget,
42 chord_key_widget,
43 GTK_TYPE_GRID)
44
45 #if 0
46 static gboolean
47 chord_key_draw_cb (
48 GtkWidget * widget,
49 cairo_t *cr,
50 ChordKeyWidget * self)
51 {
52 GtkStyleContext *context =
53 gtk_widget_get_style_context (widget);
54
55 int width =
56 gtk_widget_get_allocated_width (widget);
57 int height =
58 gtk_widget_get_allocated_height (widget);
59
60 gtk_render_background (
61 context, cr, 0, 0, width, height);
62
63 char str[100];
64 chord_descriptor_to_string (self->descr, str);
65 cairo_set_source_rgba (
66 cr, 1,1,1,1);
67 PangoLayout * layout =
68 z_cairo_create_default_pango_layout (
69 widget);
70 z_cairo_draw_text (
71 cr, widget, layout, str);
72 g_object_unref (layout);
73
74 return FALSE;
75 }
76 #endif
77
78 static void
79 on_choose_chord_btn_clicked (
80 GtkButton * btn,
81 ChordKeyWidget * self)
82 {
83 ChordSelectorWindowWidget * chord_selector =
84 chord_selector_window_widget_new (
85 self->descr);
86
87 gtk_window_present (
88 GTK_WINDOW (chord_selector));
89 }
90
91 void
chord_key_widget_refresh(ChordKeyWidget * self)92 chord_key_widget_refresh (
93 ChordKeyWidget * self)
94 {
95 char str[120];
96 chord_descriptor_to_string (self->descr, str);
97 gtk_label_set_text (self->chord_lbl, str);
98
99 piano_keyboard_widget_refresh (self->piano);
100 }
101
102 /**
103 * Creates a ChordKeyWidget for the given
104 * MIDI note descriptor.
105 */
106 ChordKeyWidget *
chord_key_widget_new(ChordDescriptor * descr)107 chord_key_widget_new (
108 ChordDescriptor * descr)
109 {
110 ChordKeyWidget * self =
111 g_object_new (CHORD_KEY_WIDGET_TYPE, NULL);
112
113 self->descr = descr;
114
115 /* add piano widget */
116 self->piano =
117 piano_keyboard_widget_new_for_chord_key (descr);
118 gtk_widget_set_size_request (
119 GTK_WIDGET (self->piano), 216, 24);
120 gtk_widget_set_visible (
121 GTK_WIDGET (self->piano), true);
122 gtk_container_add (
123 GTK_CONTAINER (self->piano_box),
124 GTK_WIDGET (self->piano));
125
126 chord_key_widget_refresh (self);
127
128 return self;
129 }
130
131 static void
chord_key_widget_class_init(ChordKeyWidgetClass * _klass)132 chord_key_widget_class_init (
133 ChordKeyWidgetClass * _klass)
134 {
135 GtkWidgetClass * klass = GTK_WIDGET_CLASS (_klass);
136 gtk_widget_class_set_css_name (
137 klass, "chord-key");
138
139 resources_set_class_template (
140 klass, "chord_key.ui");
141
142 #define BIND_CHILD(x) \
143 gtk_widget_class_bind_template_child ( \
144 klass, ChordKeyWidget, x)
145
146 BIND_CHILD (chord_lbl);
147 BIND_CHILD (piano_box);
148 BIND_CHILD (btn_box);
149 BIND_CHILD (choose_chord_btn);
150 BIND_CHILD (invert_prev_btn);
151 BIND_CHILD (invert_next_btn);
152
153 #undef BIND_CHILD
154 }
155
156 static void
chord_key_widget_init(ChordKeyWidget * self)157 chord_key_widget_init (
158 ChordKeyWidget * self)
159 {
160 gtk_widget_init_template (GTK_WIDGET (self));
161
162 gtk_widget_set_visible (
163 GTK_WIDGET (self), 1);
164
165 gtk_widget_set_halign (
166 GTK_WIDGET (self->btn_box), GTK_ALIGN_END);
167
168
169 g_signal_connect (
170 G_OBJECT (self->choose_chord_btn), "clicked",
171 G_CALLBACK (on_choose_chord_btn_clicked), self);
172 }
173