Vocoder: bugfixes
[calf.git] / src / custom_ctl.cpp
blob0025263ae3730123c0930c0d9629e5da2cf6a911
1 /* Calf DSP Library
2 * Custom controls (line graph, knob).
3 * Copyright (C) 2007-2010 Krzysztof Foltman, Torben Hohn, Markus Schmidt
4 * and others
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 #include "config.h"
22 #include <calf/giface.h>
23 #include <calf/custom_ctl.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <math.h>
26 #include <gdk/gdk.h>
27 #include <gtk/gtk.h>
28 #include <sys/time.h>
29 #include <iostream>
31 using namespace calf_plugins;
32 using namespace dsp;
34 ///////////////////////////////////////// utility functions ///////////////////////////////////////////////
36 void line_graph_background(cairo_t* c, int x, int y, int sx, int sy, int ox, int oy, float brightness, int shadow, float lights, float dull)
38 float br = brightness * 0.5 + 0.5;
40 // outer frame (black)
41 int pad = 0;
43 cairo_rectangle(
44 c, pad + x, pad + y, sx + ox * 2 - pad * 2, sy + oy * 2 - pad * 2);
45 cairo_set_source_rgb(c, 0, 0, 0);
46 cairo_fill(c);
48 // black light effect
49 pad = 1;
50 cairo_rectangle(
51 c, pad + x, pad + y, sx + ox * 2 - pad * 2, sy + oy * 2 - pad * 2);
52 cairo_pattern_t *pat2 = cairo_pattern_create_linear (
53 x, y, x, y + sy + oy * 2 - pad * 2);
54 cairo_pattern_add_color_stop_rgba (pat2, 0, 0.23, 0.23, 0.23, 1);
55 cairo_pattern_add_color_stop_rgba (pat2, 0.33, 0.13, 0.13, 0.13, 1);
56 cairo_pattern_add_color_stop_rgba (pat2, 0.33, 0.05, 0.05, 0.05, 1);
57 cairo_pattern_add_color_stop_rgba (pat2, 0.5, 0, 0, 0, 1);
58 cairo_set_source (c, pat2);
59 cairo_fill(c);
60 cairo_pattern_destroy(pat2);
62 cairo_rectangle(c, x + ox - 1, y + oy - 1, sx + 2, sy + 2);
63 cairo_set_source_rgb (c, 0, 0, 0);
64 cairo_fill(c);
66 // inner yellowish screen
67 cairo_pattern_t *pt = cairo_pattern_create_linear(x + ox, y + oy, x + ox, y + sy);
68 cairo_pattern_add_color_stop_rgb(pt, 0.0, br * 0.71, br * 0.82, br * 0.33);
69 cairo_pattern_add_color_stop_rgb(pt, 1.0, br * 0.89, br * 1.00, br * 0.54);
70 cairo_set_source (c, pt);
71 cairo_rectangle(c, x + ox, y + oy, sx, sy);
72 cairo_fill(c);
73 cairo_pattern_destroy(pt);
75 if (shadow) {
76 // top shadow
77 pt = cairo_pattern_create_linear(x + ox, y + oy, x + ox, y + oy + shadow);
78 cairo_pattern_add_color_stop_rgba(pt, 0.0, 0,0,0,0.6);
79 cairo_pattern_add_color_stop_rgba(pt, 1.0, 0,0,0,0);
80 cairo_set_source (c, pt);
81 cairo_rectangle(c, x + ox, y + oy, sx, shadow);
82 cairo_fill(c);
83 cairo_pattern_destroy(pt);
85 // left shadow
86 pt = cairo_pattern_create_linear(x + ox, y + oy, x + ox + (float)shadow * 0.7, y + oy);
87 cairo_pattern_add_color_stop_rgba(pt, 0.0, 0,0,0,0.3);
88 cairo_pattern_add_color_stop_rgba(pt, 1.0, 0,0,0,0);
89 cairo_set_source (c, pt);
90 cairo_rectangle(c, x + ox, y + oy, (float)shadow * 0.7, sy);
91 cairo_fill(c);
92 cairo_pattern_destroy(pt);
94 // right shadow
95 pt = cairo_pattern_create_linear(x + ox + sx - (float)shadow * 0.7, y + oy, x + ox + sx, y + oy);
96 cairo_pattern_add_color_stop_rgba(pt, 0.0, 0,0,0,0);
97 cairo_pattern_add_color_stop_rgba(pt, 1.0, 0,0,0,0.3);
98 cairo_set_source (c, pt);
99 cairo_rectangle(c, x + ox + sx - (float)shadow * 0.7, y + oy, 5, sy);
100 cairo_fill(c);
101 cairo_pattern_destroy(pt);
104 if(dull) {
105 // left dull
106 pt = cairo_pattern_create_linear(x + ox, y + oy, x + ox + sx / 2, y + oy);
107 cairo_pattern_add_color_stop_rgba(pt, 0.0, 0,0,0,dull);
108 cairo_pattern_add_color_stop_rgba(pt, 1.0, 0,0,0,0);
109 cairo_set_source (c, pt);
110 cairo_rectangle(c, x + ox, y + oy, sx / 2, sy);
111 cairo_fill(c);
112 cairo_pattern_destroy(pt);
114 // right dull
115 pt = cairo_pattern_create_linear(x + ox + sx / 2, y + oy, x + ox + sx, y + oy);
116 cairo_pattern_add_color_stop_rgba(pt, 0.0, 0,0,0,0);
117 cairo_pattern_add_color_stop_rgba(pt, 1.0, 0,0,0,dull);
118 cairo_set_source (c, pt);
119 cairo_rectangle(c, x + ox + sx / 2, y + oy, sx / 2, sy);
120 cairo_fill(c);
121 cairo_pattern_destroy(pt);
124 if(lights > 0) {
125 // light sources
126 int div = 1;
127 int light_w = sx;
128 while(light_w / div > 300)
129 div += 1;
130 int w = sx / div;
131 cairo_rectangle(c, x + ox, y + oy, sx, sy);
132 for(int i = 0; i < div; i ++) {
133 cairo_pattern_t *pt = cairo_pattern_create_radial(
134 x + ox + w * i + w / 2.f, y + oy, 1,
135 x + ox + w * i + w / 2.f, y + ox + sy * 0.25, w / 2.f);
136 cairo_pattern_add_color_stop_rgba (pt, 0, 1, 1, 0.8, lights);
137 cairo_pattern_add_color_stop_rgba (pt, 1, 0.89, 1.00, 0.45, 0);
138 cairo_set_source (c, pt);
139 cairo_fill_preserve(c);
140 pt = cairo_pattern_create_radial(
141 x + ox + w * i + w / 2.f, y + oy + sy, 1,
142 x + ox + w * i + w / 2.f, y + ox + sy * 0.75, w / 2.f);
143 cairo_pattern_add_color_stop_rgba (pt, 0, 1, 1, 0.8, lights);
144 cairo_pattern_add_color_stop_rgba (pt, 1, 0.89, 1.00, 0.45, 0);
145 cairo_set_source (c, pt);
146 cairo_fill_preserve(c);
151 ///////////////////////////////////////// phase graph ///////////////////////////////////////////////
153 static void
154 calf_phase_graph_draw_background( cairo_t *ctx, int sx, int sy, int ox, int oy )
156 int cx = ox + sx / 2;
157 int cy = oy + sy / 2;
159 line_graph_background(ctx, 0, 0, sx, sy, ox, oy);
160 cairo_set_source_rgb(ctx, 0.35, 0.4, 0.2);
161 cairo_select_font_face(ctx, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
162 cairo_set_font_size(ctx, 9);
163 cairo_text_extents_t te;
165 cairo_text_extents (ctx, "M", &te);
166 cairo_move_to (ctx, cx + 5, oy + 12);
167 cairo_show_text (ctx, "M");
169 cairo_text_extents (ctx, "S", &te);
170 cairo_move_to (ctx, ox + 5, cy - 5);
171 cairo_show_text (ctx, "S");
173 cairo_text_extents (ctx, "L", &te);
174 cairo_move_to (ctx, ox + 18, oy + 12);
175 cairo_show_text (ctx, "L");
177 cairo_text_extents (ctx, "R", &te);
178 cairo_move_to (ctx, ox + sx - 22, oy + 12);
179 cairo_show_text (ctx, "R");
181 cairo_set_line_width(ctx, 1);
183 cairo_move_to(ctx, ox, oy + sy * 0.5);
184 cairo_line_to(ctx, ox + sx, oy + sy * 0.5);
185 cairo_stroke(ctx);
187 cairo_move_to(ctx, ox + sx * 0.5, oy);
188 cairo_line_to(ctx, ox + sx * 0.5, oy + sy);
189 cairo_stroke(ctx);
191 cairo_set_source_rgba(ctx, 0, 0, 0, 0.2);
192 cairo_move_to(ctx, ox, oy);
193 cairo_line_to(ctx, ox + sx, oy + sy);
194 cairo_stroke(ctx);
196 cairo_move_to(ctx, ox, oy + sy);
197 cairo_line_to(ctx, ox + sx, oy);
198 cairo_stroke(ctx);
200 static void
201 calf_phase_graph_copy_surface(cairo_t *ctx, cairo_surface_t *source, float fade = 1.f)
203 // copy a surface to a cairo context
204 cairo_save(ctx);
205 cairo_set_source_surface(ctx, source, 0, 0);
206 if (fade < 1.0) {
207 float rnd = (float)rand() / (float)RAND_MAX / 100;
208 cairo_paint_with_alpha(ctx, fade * 0.35 + 0.05 + rnd);
209 } else {
210 cairo_paint(ctx);
212 cairo_restore(ctx);
214 static gboolean
215 calf_phase_graph_expose (GtkWidget *widget, GdkEventExpose *event)
217 g_assert(CALF_IS_PHASE_GRAPH(widget));
218 CalfPhaseGraph *pg = CALF_PHASE_GRAPH(widget);
219 if (!pg->source)
220 return FALSE;
222 // dimensions
223 int ox = 5, oy = 5;
224 int sx = widget->allocation.width - ox * 2, sy = widget->allocation.height - oy * 2;
225 sx += sx % 2 - 1;
226 sy += sy % 2 - 1;
227 int rad = sx / 2;
228 int cx = ox + sx / 2;
229 int cy = oy + sy / 2;
231 // some values as pointers for the audio plug-in call
232 std::string legend;
233 float *data = new float[2 * sx];
234 float * phase_buffer = 0;
235 int length = 0;
236 int mode = 2;
237 float fade = 0.05;
238 bool use_fade = true;
239 int accuracy = 1;
240 bool display = true;
242 // cairo initialization stuff
243 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(widget->window));
244 cairo_t *ctx_back;
245 cairo_t *ctx_cache;
247 if( pg->background == NULL ) {
248 // looks like its either first call or the widget has been resized.
249 // create the background surface (stolen from line graph)...
250 cairo_surface_t *window_surface = cairo_get_target(c);
251 pg->background = cairo_surface_create_similar(window_surface,
252 CAIRO_CONTENT_COLOR,
253 widget->allocation.width,
254 widget->allocation.height );
255 pg->cache = cairo_surface_create_similar(window_surface,
256 CAIRO_CONTENT_COLOR,
257 widget->allocation.width,
258 widget->allocation.height );
260 // ...and draw some bling bling onto it...
261 ctx_back = cairo_create(pg->background);
262 calf_phase_graph_draw_background(ctx_back, sx, sy, ox, oy);
263 // ...and copy it to the cache
264 ctx_cache = cairo_create(pg->cache);
265 calf_phase_graph_copy_surface(ctx_cache, pg->background, 1);
266 } else {
267 ctx_back = cairo_create(pg->background);
268 ctx_cache = cairo_create(pg->cache);
271 pg->source->get_phase_graph(&phase_buffer, &length, &mode, &use_fade,
272 &fade, &accuracy, &display);
274 // process some values set by the plug-in
275 accuracy *= 2;
276 accuracy = 12 - accuracy;
278 calf_phase_graph_copy_surface(ctx_cache, pg->background, use_fade ? fade : 1);
280 if(display) {
281 cairo_rectangle(ctx_cache, ox, oy, sx, sy);
282 cairo_clip(ctx_cache);
283 cairo_set_source_rgba(ctx_cache, 0.35, 0.4, 0.2, 1);
284 double _a;
285 for(int i = 0; i < length; i+= accuracy) {
286 float l = phase_buffer[i];
287 float r = phase_buffer[i + 1];
288 if(l == 0.f and r == 0.f) continue;
289 else if(r == 0.f and l > 0.f) _a = M_PI / 2.f;
290 else if(r == 0.f and l < 0.f) _a = 3.f *M_PI / 2.f;
291 else _a = pg->_atan(l / r, l, r);
292 double _R = sqrt(pow(l, 2) + pow(r, 2));
293 _a += M_PI / 4.f;
294 float x = (-1.f)*_R * cos(_a);
295 float y = _R * sin(_a);
296 // mask the cached values
297 switch(mode) {
298 case 0:
299 // small dots
300 cairo_rectangle (ctx_cache, x * rad + cx, y * rad + cy, 1, 1);
301 break;
302 case 1:
303 // medium dots
304 cairo_rectangle (ctx_cache, x * rad + cx - 0.25, y * rad + cy - 0.25, 1.5, 1.5);
305 break;
306 case 2:
307 // big dots
308 cairo_rectangle (ctx_cache, x * rad + cx - 0.5, y * rad + cy - 0.5, 2, 2);
309 break;
310 case 3:
311 // fields
312 if(i == 0) cairo_move_to(ctx_cache,
313 x * rad + cx, y * rad + cy);
314 else cairo_line_to(ctx_cache,
315 x * rad + cx, y * rad + cy);
316 break;
317 case 4:
318 // lines
319 if(i == 0) cairo_move_to(ctx_cache,
320 x * rad + cx, y * rad + cy);
321 else cairo_line_to(ctx_cache,
322 x * rad + cx, y * rad + cy);
323 break;
326 // draw
327 switch(mode) {
328 case 0:
329 case 1:
330 case 2:
331 cairo_fill(ctx_cache);
332 break;
333 case 3:
334 cairo_fill(ctx_cache);
335 break;
336 case 4:
337 cairo_set_line_width(ctx_cache, 0.5);
338 cairo_stroke(ctx_cache);
339 break;
343 calf_phase_graph_copy_surface(c, pg->cache, 1);
345 cairo_destroy(c);
346 cairo_destroy(ctx_back);
347 cairo_destroy(ctx_cache);
348 delete []data;
349 // printf("exposed %p %dx%d %d+%d\n", widget->window, event->area.x, event->area.y, event->area.width, event->area.height);
350 return TRUE;
353 static void
354 calf_phase_graph_size_request (GtkWidget *widget,
355 GtkRequisition *requisition)
357 g_assert(CALF_IS_PHASE_GRAPH(widget));
358 // CalfLineGraph *lg = CALF_LINE_GRAPH(widget);
361 static void
362 calf_phase_graph_size_allocate (GtkWidget *widget,
363 GtkAllocation *allocation)
365 g_assert(CALF_IS_PHASE_GRAPH(widget));
366 CalfPhaseGraph *lg = CALF_PHASE_GRAPH(widget);
368 GtkWidgetClass *parent_class = (GtkWidgetClass *) g_type_class_peek_parent( CALF_PHASE_GRAPH_GET_CLASS( lg ) );
370 if(lg->background)
371 cairo_surface_destroy(lg->background);
372 lg->background = NULL;
374 widget->allocation = *allocation;
375 GtkAllocation &a = widget->allocation;
376 if (a.width > a.height) {
377 a.x += (a.width - a.height) / 2;
378 a.width = a.height;
380 if (a.width < a.height) {
381 a.y += (a.height - a.width) / 2;
382 a.height = a.width;
384 parent_class->size_allocate(widget, &a);
387 static void
388 calf_phase_graph_class_init (CalfPhaseGraphClass *klass)
390 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
391 widget_class->expose_event = calf_phase_graph_expose;
392 widget_class->size_request = calf_phase_graph_size_request;
393 widget_class->size_allocate = calf_phase_graph_size_allocate;
396 static void
397 calf_phase_graph_unrealize (GtkWidget *widget, CalfPhaseGraph *pg)
399 if( pg->background )
400 cairo_surface_destroy(pg->background);
401 pg->background = NULL;
404 static void
405 calf_phase_graph_init (CalfPhaseGraph *self)
407 GtkWidget *widget = GTK_WIDGET(self);
408 widget->requisition.width = 40;
409 widget->requisition.height = 40;
410 self->background = NULL;
411 g_signal_connect(GTK_OBJECT(widget), "unrealize", G_CALLBACK(calf_phase_graph_unrealize), (gpointer)self);
414 GtkWidget *
415 calf_phase_graph_new()
417 return GTK_WIDGET(g_object_new (CALF_TYPE_PHASE_GRAPH, NULL));
420 GType
421 calf_phase_graph_get_type (void)
423 static GType type = 0;
424 if (!type) {
425 static const GTypeInfo type_info = {
426 sizeof(CalfPhaseGraphClass),
427 NULL, /* base_init */
428 NULL, /* base_finalize */
429 (GClassInitFunc)calf_phase_graph_class_init,
430 NULL, /* class_finalize */
431 NULL, /* class_data */
432 sizeof(CalfPhaseGraph),
433 0, /* n_preallocs */
434 (GInstanceInitFunc)calf_phase_graph_init
437 GTypeInfo *type_info_copy = new GTypeInfo(type_info);
439 for (int i = 0; ; i++) {
440 char *name = g_strdup_printf("CalfPhaseGraph%u%d", ((unsigned int)(intptr_t)calf_phase_graph_class_init) >> 16, i);
441 if (g_type_from_name(name)) {
442 free(name);
443 continue;
445 type = g_type_register_static( GTK_TYPE_DRAWING_AREA,
446 name,
447 type_info_copy,
448 (GTypeFlags)0);
449 free(name);
450 break;
453 return type;
457 ///////////////////////////////////////// toggle ///////////////////////////////////////////////
459 static gboolean
460 calf_toggle_expose (GtkWidget *widget, GdkEventExpose *event)
462 g_assert(CALF_IS_TOGGLE(widget));
464 CalfToggle *self = CALF_TOGGLE(widget);
466 int x = widget->allocation.x + widget->allocation.width / 2 - self->size * 15 - self->size * 2;
467 int y = widget->allocation.y + widget->allocation.height / 2 - self->size * 10 - self->size * 3;
468 int width = self->size * 34;
469 int height = self->size * 26;
471 gdk_draw_pixbuf(GDK_DRAWABLE(widget->window),
472 widget->style->fg_gc[0],
473 self->toggle_image[self->size - 1],
474 20 - self->size * 2,
475 20 - self->size * 3 + (self->size * 20 + 40) * floor(.5 + gtk_range_get_value(GTK_RANGE(widget))),
478 width,
479 height,
480 GDK_RGB_DITHER_NORMAL, 0, 0);
481 return TRUE;
484 static void
485 calf_toggle_size_request (GtkWidget *widget,
486 GtkRequisition *requisition)
488 g_assert(CALF_IS_TOGGLE(widget));
490 CalfToggle *self = CALF_TOGGLE(widget);
492 requisition->width = 30 * self->size;
493 requisition->height = 20 * self->size;
496 static gboolean
497 calf_toggle_button_press (GtkWidget *widget, GdkEventButton *event)
499 g_assert(CALF_IS_TOGGLE(widget));
500 GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(widget));
501 if (gtk_range_get_value(GTK_RANGE(widget)) == adj->lower)
503 gtk_range_set_value(GTK_RANGE(widget), adj->upper);
504 } else {
505 gtk_range_set_value(GTK_RANGE(widget), adj->lower);
507 return TRUE;
510 static gboolean
511 calf_toggle_key_press (GtkWidget *widget, GdkEventKey *event)
513 switch(event->keyval)
515 case GDK_Return:
516 case GDK_KP_Enter:
517 case GDK_space:
518 return calf_toggle_button_press(widget, NULL);
520 return FALSE;
523 static void
524 calf_toggle_class_init (CalfToggleClass *klass)
526 // GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
527 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
528 widget_class->expose_event = calf_toggle_expose;
529 widget_class->size_request = calf_toggle_size_request;
530 widget_class->button_press_event = calf_toggle_button_press;
531 widget_class->key_press_event = calf_toggle_key_press;
534 static void
535 calf_toggle_init (CalfToggle *self)
537 GtkWidget *widget = GTK_WIDGET(self);
538 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(self), GTK_CAN_FOCUS);
539 widget->requisition.width = 30;
540 widget->requisition.height = 20;
541 self->size = 1;
542 GError *error = NULL;
543 self->toggle_image[0] = gdk_pixbuf_new_from_file(PKGLIBDIR "/toggle1_silver.png", &error);
544 self->toggle_image[1] = gdk_pixbuf_new_from_file(PKGLIBDIR "/toggle2_silver.png", &error);
545 g_assert(self->toggle_image != NULL);
548 GtkWidget *
549 calf_toggle_new()
551 GtkAdjustment *adj = (GtkAdjustment *)gtk_adjustment_new(0, 0, 1, 1, 0, 0);
552 return calf_toggle_new_with_adjustment(adj);
555 static gboolean calf_toggle_value_changed(gpointer obj)
557 GtkWidget *widget = (GtkWidget *)obj;
558 CalfToggle *self = CALF_TOGGLE(widget);
559 gtk_widget_queue_draw_area(widget,
560 widget->allocation.x - self->size * 2,
561 widget->allocation.y - self->size * 3,
562 self->size * 34,
563 self->size * 26);
564 return FALSE;
567 GtkWidget *calf_toggle_new_with_adjustment(GtkAdjustment *_adjustment)
569 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_TOGGLE, NULL ));
570 if (widget) {
571 gtk_range_set_adjustment(GTK_RANGE(widget), _adjustment);
572 g_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(calf_toggle_value_changed), widget);
574 return widget;
577 GType
578 calf_toggle_get_type (void)
580 static GType type = 0;
581 if (!type) {
583 static const GTypeInfo type_info = {
584 sizeof(CalfToggleClass),
585 NULL, /* base_init */
586 NULL, /* base_finalize */
587 (GClassInitFunc)calf_toggle_class_init,
588 NULL, /* class_finalize */
589 NULL, /* class_data */
590 sizeof(CalfToggle),
591 0, /* n_preallocs */
592 (GInstanceInitFunc)calf_toggle_init
595 for (int i = 0; ; i++) {
596 char *name = g_strdup_printf("CalfToggle%u%d",
597 ((unsigned int)(intptr_t)calf_toggle_class_init) >> 16, i);
598 if (g_type_from_name(name)) {
599 free(name);
600 continue;
602 type = g_type_register_static( GTK_TYPE_RANGE,
603 name,
604 &type_info,
605 (GTypeFlags)0);
606 free(name);
607 break;
610 return type;
613 ///////////////////////////////////////// frame ///////////////////////////////////////////////
616 GtkWidget *
617 calf_frame_new(const char *label)
619 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_FRAME, NULL ));
620 CalfFrame *self = CALF_FRAME(widget);
621 gtk_frame_set_label(GTK_FRAME(self), label);
622 return widget;
624 static gboolean
625 calf_frame_expose (GtkWidget *widget, GdkEventExpose *event)
627 g_assert(CALF_IS_FRAME(widget));
628 if (gtk_widget_is_drawable (widget)) {
630 GdkWindow *window = widget->window;
631 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
632 cairo_text_extents_t extents;
634 int ox = widget->allocation.x;
635 int oy = widget->allocation.y;
636 int sx = widget->allocation.width;
637 int sy = widget->allocation.height;
639 double rad = 8;
640 double a = 1.5;
641 double pad = 4;
642 double txp = 4;
643 double m = 1;
644 double size = 10;
646 cairo_rectangle(c, ox, oy, sx, sy);
647 cairo_clip(c);
650 const gchar *lab = gtk_frame_get_label(GTK_FRAME(widget));
652 cairo_select_font_face(c, "Sans",
653 CAIRO_FONT_SLANT_NORMAL,
654 CAIRO_FONT_WEIGHT_NORMAL);
655 cairo_set_font_size(c, size);
657 cairo_text_extents(c, lab, &extents);
659 double lw = extents.width + txp * 2.;
661 cairo_set_line_width(c, 1.);
663 cairo_move_to(c, ox + rad + txp + m, oy + size - 2 + m);
664 cairo_set_source_rgb(c, 0.99,0.99,0.99);
665 cairo_show_text(c, lab);
667 cairo_set_source_rgb(c, 0.9,0.9,0.9);
669 rad = 8;
670 cairo_move_to(c, ox + a + m, oy + pad + rad + a + m);
671 cairo_arc (c, ox + rad + a + m, oy + rad + a + pad + m, rad, 1 * M_PI, 1.5 * M_PI);
672 cairo_move_to(c, ox + rad + a + lw + m, oy + a + pad + m);
673 cairo_line_to(c, ox + sx + a - rad - m - 1, oy + a + pad + m);
674 cairo_arc (c, ox + sx - rad + a - 2*m - 1, oy + rad + a + pad + m, rad, 1.5 * M_PI, 2 * M_PI);
675 cairo_line_to(c, ox + sx + a - 2*m - 1, oy + a + sy - rad - 2*m - 1);
676 rad = 9;
677 cairo_arc (c, ox + sx - rad + a - 2*m - 1, oy + sy - rad + a - 2*m - 1, rad, 0 * M_PI, 0.5 * M_PI);
678 rad = 8;
679 cairo_line_to(c, ox + a + rad + m, oy + sy + a - 2*m - 1);
680 cairo_arc (c, ox + rad + a + m, oy + sy - rad + a - 2*m - 1, rad, 0.5 * M_PI, 1 * M_PI);
681 cairo_line_to(c, ox + a + m, oy + a + rad + pad + m);
682 cairo_stroke(c);
684 a = 0.5;
686 cairo_set_source_rgb(c, 0.66,0.66,0.66);
688 rad = 9;
689 cairo_move_to(c, ox + a + m, oy + pad + rad + a + m);
690 cairo_arc (c, ox + rad + a + m, oy + rad + a + pad + m, rad, 1 * M_PI, 1.5 * M_PI);
691 cairo_move_to(c, ox + rad + a + lw + m, oy + a + pad + m);
692 rad = 8;
693 cairo_line_to(c, ox + sx + a - rad - m, oy + a + pad + m);
694 cairo_arc (c, ox + sx - rad + a - 2*m - 1, oy + rad + a + pad + m, rad, 1.5 * M_PI, 2 * M_PI);
695 cairo_line_to(c, ox + sx + a - 2*m - 1, oy + a + sy - rad - 2*m);
696 cairo_arc (c, ox + sx - rad + a - 2*m - 1, oy + sy - rad + a - 2*m - 1, rad, 0 * M_PI, 0.5 * M_PI);
697 cairo_line_to(c, ox + a + rad + m, oy + sy + a - 2*m - 1);
698 cairo_arc (c, ox + rad + a + m, oy + sy - rad + a - 2*m - 1, rad, 0.5 * M_PI, 1 * M_PI);
699 cairo_line_to(c, ox + a + m, oy + a + rad + pad + m);
700 cairo_stroke(c);
702 cairo_destroy(c);
704 if (gtk_bin_get_child(GTK_BIN(widget))) {
705 gtk_container_propagate_expose(GTK_CONTAINER(widget),
706 gtk_bin_get_child(GTK_BIN(widget)),
707 event);
709 return FALSE;
712 static void
713 calf_frame_class_init (CalfFrameClass *klass)
715 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
716 widget_class->expose_event = calf_frame_expose;
719 static void
720 calf_frame_init (CalfFrame *self)
722 GtkWidget *widget = GTK_WIDGET(self);
723 widget->requisition.width = 40;
724 widget->requisition.height = 40;
727 GType
728 calf_frame_get_type (void)
730 static GType type = 0;
731 if (!type) {
732 static const GTypeInfo type_info = {
733 sizeof(CalfFrameClass),
734 NULL, /* base_init */
735 NULL, /* base_finalize */
736 (GClassInitFunc)calf_frame_class_init,
737 NULL, /* class_finalize */
738 NULL, /* class_data */
739 sizeof(CalfFrame),
740 0, /* n_preallocs */
741 (GInstanceInitFunc)calf_frame_init
744 for (int i = 0; ; i++) {
745 char *name = g_strdup_printf("CalfFrame%u%d",
746 ((unsigned int)(intptr_t)calf_frame_class_init) >> 16, i);
747 if (g_type_from_name(name)) {
748 free(name);
749 continue;
751 type = g_type_register_static(GTK_TYPE_FRAME,
752 name,
753 &type_info,
754 (GTypeFlags)0);
755 free(name);
756 break;
759 return type;
762 ///////////////////////////////////////// combo box ///////////////////////////////////////////////
765 GtkWidget *
766 calf_combobox_new()
768 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_COMBOBOX, NULL ));
769 return widget;
771 static gboolean
772 calf_combobox_expose (GtkWidget *widget, GdkEventExpose *event)
774 g_assert(CALF_IS_COMBOBOX(widget));
776 if (gtk_widget_is_drawable (widget)) {
778 int pad = 4;
780 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX (widget));
781 GtkTreeIter iter;
782 gchar *lab;
783 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
784 gtk_tree_model_get (model, &iter, 0, &lab, -1);
785 else
786 lab = g_strdup("");
788 GdkWindow *window = widget->window;
789 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
791 int x = widget->allocation.x;
792 int y = widget->allocation.y;
793 int sx = widget->allocation.width;
794 int sy = widget->allocation.height;
795 gint mx, my;
796 bool hover = false;
798 cairo_rectangle(c, x, y, sx, sy);
799 cairo_clip(c);
801 gtk_widget_get_pointer(GTK_WIDGET(widget), &mx, &my);
802 if (mx >= 0 and mx < sx and my >= 0 and my < sy)
803 hover = true;
805 line_graph_background(c, x, y, sx - pad * 2, sy - pad * 2, pad, pad, g_ascii_isspace(lab[0]) ? 0 : 1, 4, hover ? 0.5 : 0, hover ? 0.1 : 0.25);
807 cairo_select_font_face(c, "Sans",
808 CAIRO_FONT_SLANT_NORMAL,
809 CAIRO_FONT_WEIGHT_NORMAL);
810 cairo_set_font_size(c, 12);
812 cairo_move_to(c, x + pad + 3, y + sy / 2 + 5);
813 cairo_set_source_rgb(c, 0.,0.,0.);
814 cairo_show_text(c, lab);
815 g_free(lab);
817 cairo_surface_t *image;
818 image = cairo_image_surface_create_from_png(PKGLIBDIR "combo_arrow.png");
819 cairo_set_source_surface(c, image, x + sx - 20, y + sy / 2 - 5);
820 cairo_rectangle(c, x, y, sx, sy);
821 cairo_fill(c);
823 cairo_destroy(c);
825 return FALSE;
828 static void
829 calf_combobox_class_init (CalfComboboxClass *klass)
831 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
832 widget_class->expose_event = calf_combobox_expose;
835 static void
836 calf_combobox_init (CalfCombobox *self)
838 GtkWidget *widget = GTK_WIDGET(self);
839 widget->requisition.width = 40;
840 widget->requisition.height = 20;
843 GType
844 calf_combobox_get_type (void)
846 static GType type = 0;
847 if (!type) {
848 static const GTypeInfo type_info = {
849 sizeof(CalfComboboxClass),
850 NULL, /* base_init */
851 NULL, /* base_finalize */
852 (GClassInitFunc)calf_combobox_class_init,
853 NULL, /* class_finalize */
854 NULL, /* class_data */
855 sizeof(CalfCombobox),
856 0, /* n_preallocs */
857 (GInstanceInitFunc)calf_combobox_init
860 for (int i = 0; ; i++) {
861 char *name = g_strdup_printf("CalfCombobox%u%d",
862 ((unsigned int)(intptr_t)calf_combobox_class_init) >> 16, i);
863 if (g_type_from_name(name)) {
864 free(name);
865 continue;
867 type = g_type_register_static(GTK_TYPE_COMBO_BOX_TEXT,
868 name,
869 &type_info,
870 (GTypeFlags)0);
871 free(name);
872 break;
875 return type;
879 ///////////////////////////////////////// notebook ///////////////////////////////////////////////
881 #define GTK_NOTEBOOK_PAGE(_glist_) ((GtkNotebookPage *)((GList *)(_glist_))->data)
882 struct _GtkNotebookPage
884 GtkWidget *child;
885 GtkWidget *tab_label;
886 GtkWidget *menu_label;
887 GtkWidget *last_focus_child; /* Last descendant of the page that had focus */
889 guint default_menu : 1; /* If true, we create the menu label ourself */
890 guint default_tab : 1; /* If true, we create the tab label ourself */
891 guint expand : 1;
892 guint fill : 1;
893 guint pack : 1;
894 guint reorderable : 1;
895 guint detachable : 1;
897 /* if true, the tab label was visible on last allocation; we track this so
898 * that we know to redraw the tab area if a tab label was hidden then shown
899 * without changing position */
900 guint tab_allocated_visible : 1;
902 GtkRequisition requisition;
903 GtkAllocation allocation;
905 gulong mnemonic_activate_signal;
906 gulong notify_visible_handler;
909 GtkWidget *
910 calf_notebook_new()
912 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_NOTEBOOK, NULL ));
913 return widget;
915 static gboolean
916 calf_notebook_expose (GtkWidget *widget, GdkEventExpose *event)
918 g_assert(CALF_IS_NOTEBOOK(widget));
920 GtkNotebook *notebook;
921 notebook = GTK_NOTEBOOK (widget);
922 CalfNotebookClass *klass = CALF_NOTEBOOK_CLASS(GTK_OBJECT_GET_CLASS(widget));
924 if (gtk_widget_is_drawable (widget)) {
926 GdkWindow *window = widget->window;
927 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
928 cairo_pattern_t *pat = NULL;
930 int x = widget->allocation.x;
931 int y = widget->allocation.y;
932 int sx = widget->allocation.width;
933 int sy = widget->allocation.height;
934 int tx = widget->style->xthickness;
935 int ty = widget->style->ythickness;
936 int lh = 19;
937 int bh = lh + 2 * ty;
939 cairo_rectangle(c, x, y, sx, sy);
940 cairo_clip(c);
942 int add = 0;
944 if (notebook->show_tabs) {
945 GtkNotebookPage *page;
946 GList *pages;
948 gint sp;
949 gtk_widget_style_get(widget, "tab-overlap", &sp, NULL);
951 pages = notebook->children;
953 int cn = 0;
954 while (pages) {
955 page = GTK_NOTEBOOK_PAGE (pages);
956 pages = pages->next;
957 if (page->tab_label->window == event->window &&
958 gtk_widget_is_drawable (page->tab_label)) {
959 int lx = page->tab_label->allocation.x;
960 int lw = page->tab_label->allocation.width;
962 // fix the labels position
963 page->tab_label->allocation.y = y + ty;
964 page->tab_label->allocation.height = lh;
966 // draw tab background
967 cairo_rectangle(c, lx - tx, y, lw + 2 * tx, bh);
968 cairo_set_source_rgba(c, 0,0,0, page != notebook->cur_page ? 0.25 : 0.5);
969 cairo_fill(c);
971 if (page == notebook->cur_page) {
972 // draw tab light
973 cairo_rectangle(c, lx - tx + 2, y + 2, lw + 2 * tx - 4, 2);
974 pat = cairo_pattern_create_radial(lx + lw / 2, y + bh / 2, 1, lx + lw / 2, y + bh / 2, lw + tx * 2);
975 cairo_pattern_add_color_stop_rgb(pat, 0, 50. / 255, 1, 1);
976 cairo_pattern_add_color_stop_rgb(pat, 0.3, 2. / 255, 180. / 255, 1);
977 cairo_pattern_add_color_stop_rgb(pat, 0.5, 19. / 255, 220. / 255, 1);
978 cairo_pattern_add_color_stop_rgb(pat, 1, 2. / 255, 120. / 255, 1);
979 cairo_set_source(c, pat);
980 cairo_fill(c);
982 cairo_rectangle(c, lx - tx + 2, y + 1, lw + 2 * tx - 4, 1);
983 cairo_set_source_rgba(c, 0,0,0,0.5);
984 cairo_fill(c);
986 cairo_rectangle(c, lx - tx + 2, y + 4, lw + 2 * tx - 4, 1);
987 cairo_set_source_rgba(c, 1,1,1,0.3);
988 cairo_fill(c);
991 // draw labels
992 gtk_container_propagate_expose (GTK_CONTAINER (notebook), page->tab_label, event);
994 cn++;
996 add = bh;
999 // draw main body
1000 cairo_rectangle(c, x, y + add, sx, sy - add);
1001 cairo_set_source_rgba(c, 0,0,0,0.5);
1002 cairo_fill(c);
1004 // draw frame
1005 cairo_rectangle(c, x + 0.5, y + add + 0.5, sx - 1, sy - add - 1);
1006 pat = cairo_pattern_create_linear(x, y + add, x, y + sy - add);
1007 cairo_pattern_add_color_stop_rgba(pat, 0, 0, 0, 0, 0.3);
1008 cairo_pattern_add_color_stop_rgba(pat, 0.5, 0.5, 0.5, 0.5, 0);
1009 cairo_pattern_add_color_stop_rgba(pat, 1, 1, 1, 1, 0.2);
1010 cairo_set_source (c, pat);
1011 cairo_set_line_width(c, 1);
1012 cairo_stroke_preserve(c);
1014 int sw = gdk_pixbuf_get_width(klass->screw);
1015 int sh = gdk_pixbuf_get_height(klass->screw);
1017 // draw screws
1018 gdk_cairo_set_source_pixbuf(c, klass->screw, x, y + add);
1019 cairo_fill_preserve(c);
1020 gdk_cairo_set_source_pixbuf(c, klass->screw, x + sx - sw, y + add);
1021 cairo_fill_preserve(c);
1022 gdk_cairo_set_source_pixbuf(c, klass->screw, x, y + sy - sh);
1023 cairo_fill_preserve(c);
1024 gdk_cairo_set_source_pixbuf(c, klass->screw, x + sx - sh, y + sy - sh);
1025 cairo_fill_preserve(c);
1027 // propagate expose to all children
1028 if (notebook->cur_page)
1029 gtk_container_propagate_expose (GTK_CONTAINER (notebook),
1030 notebook->cur_page->child,
1031 event);
1033 cairo_pattern_destroy(pat);
1034 cairo_destroy(c);
1037 return FALSE;
1040 static void
1041 calf_notebook_class_init (CalfNotebookClass *klass)
1043 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1044 widget_class->expose_event = calf_notebook_expose;
1046 klass->screw = gdk_pixbuf_new_from_file (PKGLIBDIR "screw_black.png", 0);
1049 static void
1050 calf_notebook_init (CalfNotebook *self)
1052 //GtkWidget *widget = GTK_WIDGET(self);
1055 GType
1056 calf_notebook_get_type (void)
1058 static GType type = 0;
1059 if (!type) {
1060 static const GTypeInfo type_info = {
1061 sizeof(CalfNotebookClass),
1062 NULL, /* base_init */
1063 NULL, /* base_finalize */
1064 (GClassInitFunc)calf_notebook_class_init,
1065 NULL, /* class_finalize */
1066 NULL, /* class_data */
1067 sizeof(CalfNotebook),
1068 0, /* n_preallocs */
1069 (GInstanceInitFunc)calf_notebook_init
1072 for (int i = 0; ; i++) {
1073 char *name = g_strdup_printf("CalfNotebook%u%d",
1074 ((unsigned int)(intptr_t)calf_notebook_class_init) >> 16, i);
1075 if (g_type_from_name(name)) {
1076 free(name);
1077 continue;
1079 type = g_type_register_static(GTK_TYPE_NOTEBOOK,
1080 name,
1081 &type_info,
1082 (GTypeFlags)0);
1083 free(name);
1084 break;
1087 return type;
1090 ///////////////////////////////////////// fader ///////////////////////////////////////////////
1092 static void calf_fader_set_layout(GtkWidget *widget)
1094 GtkRange *range = GTK_RANGE(widget);
1095 CalfFader *fader = CALF_FADER(widget);
1096 CalfFaderLayout layout = fader->layout;
1098 int hor = fader->horizontal;
1100 // widget layout
1101 layout.x = widget->allocation.x;
1102 layout.y = widget->allocation.y;
1103 layout.w = widget->allocation.width;
1104 layout.h = widget->allocation.height;
1106 // trough layout
1107 layout.tx = range->range_rect.x + layout.x;
1108 layout.ty = range->range_rect.y + layout.y;
1109 layout.tw = range->range_rect.width;
1110 layout.th = range->range_rect.height;
1111 layout.tc = hor ? layout.ty + layout.th / 2 : layout.tx + layout.tw / 2;
1113 // screw layout
1114 layout.scw = gdk_pixbuf_get_width(fader->screw);
1115 layout.sch = gdk_pixbuf_get_height(fader->screw);
1116 layout.scx1 = hor ? layout.tx : layout.tc - layout.scw / 2;
1117 layout.scy1 = hor ? layout.tc - layout.sch / 2 : layout.ty;
1118 layout.scx2 = hor ? layout.tx + layout.tw - layout.scw : layout.tc - layout.scw / 2;
1119 layout.scy2 = hor ? layout.tc - layout.sch / 2 : layout.ty + layout.th - layout.sch;
1121 // slit layout
1122 layout.sw = hor ? layout.tw - layout.scw * 2 - 2: 2;
1123 layout.sh = hor ? 2 : layout.th - layout.sch * 2 - 2;
1124 layout.sx = hor ? layout.tx + layout.scw + 1 : layout.tc - 1;
1125 layout.sy = hor ? layout.tc - 1 : layout.ty + layout.sch + 1;
1127 // slider layout
1128 layout.slw = gdk_pixbuf_get_width(fader->slider);
1129 layout.slh = gdk_pixbuf_get_height(fader->slider);
1130 layout.slx = fader->horizontal ? 0 : layout.tc - layout.slw / 2;
1131 layout.sly = fader->horizontal ? layout.tc - layout.slh / 2 : 0;
1133 //printf("widg %d %d %d %d | trgh %d %d %d %d %d | slid %d %d %d %d | slit %d %d %d %d\n",
1134 //layout.x, layout.y, layout.w, layout.h,
1135 //layout.tx, layout.ty, layout.tw, layout.th, layout.tc,
1136 //layout.slx, layout.sly, layout.slw, layout.slh,
1137 //layout.sx, layout.sy, layout.sw, layout.sh);
1139 fader->layout = layout;
1142 GtkWidget *
1143 calf_fader_new(const int horiz = 0, const int size = 2, const double min = 0, const double max = 1, const double step = 0.1)
1145 GtkObject *adj;
1146 gint digits;
1148 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
1150 if (fabs (step) >= 1.0 || step == 0.0)
1151 digits = 0;
1152 else
1153 digits = std::min(5, abs((gint) floor (log10 (fabs (step)))));
1155 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_FADER, NULL ));
1156 CalfFader *self = CALF_FADER(widget);
1158 GTK_RANGE(widget)->orientation = horiz ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL;
1159 gtk_range_set_adjustment(GTK_RANGE(widget), GTK_ADJUSTMENT(adj));
1160 gtk_scale_set_digits(GTK_SCALE(widget), digits);
1162 self->size = size;
1163 self->horizontal = horiz;
1164 self->hover = 0;
1166 gchar * file = g_strdup_printf("%sslider%d-%s.png", PKGLIBDIR, size, horiz ? "horiz" : "vert");
1167 self->slider = gdk_pixbuf_new_from_file (file, 0);
1168 file = g_strdup_printf("%sslider%d-%s-prelight.png", PKGLIBDIR, size, horiz ? "horiz" : "vert");
1169 self->sliderpre = gdk_pixbuf_new_from_file(file, 0);
1171 self->screw = gdk_pixbuf_new_from_file (PKGLIBDIR "screw_silver.png", 0);
1173 return widget;
1176 static bool calf_fader_hover(GtkWidget *widget)
1178 CalfFader *fader = CALF_FADER(widget);
1179 CalfFaderLayout layout = fader->layout;
1180 gint mx, my;
1181 gtk_widget_get_pointer(GTK_WIDGET(widget), &mx, &my);
1183 if ((fader->horizontal and mx + layout.x >= layout.tx
1184 and mx + layout.x < layout.tx + layout.tw
1185 and my + layout.y >= layout.sly
1186 and my + layout.y < layout.sly + layout.slh)
1187 or (!fader->horizontal and mx + layout.x >= layout.slx
1188 and mx + layout.x < layout.slx + layout.slw
1189 and my + layout.y >= layout.ty
1190 and my + layout.y < layout.ty + layout.th))
1191 return true;
1192 return false;
1194 static void calf_fader_check_hover_change(GtkWidget *widget)
1196 CalfFader *fader = CALF_FADER(widget);
1197 bool hover = calf_fader_hover(widget);
1198 if (hover != fader->hover)
1199 gtk_widget_queue_draw(widget);
1200 fader->hover = hover;
1202 static gboolean
1203 calf_fader_motion (GtkWidget *widget, GdkEventMotion *event)
1205 calf_fader_check_hover_change(widget);
1206 return FALSE;
1209 static gboolean
1210 calf_fader_enter (GtkWidget *widget, GdkEventCrossing *event)
1212 calf_fader_check_hover_change(widget);
1213 return FALSE;
1216 static gboolean
1217 calf_fader_leave (GtkWidget *widget, GdkEventCrossing *event)
1219 CALF_FADER(widget)->hover = false;
1220 gtk_widget_queue_draw(widget);
1221 return FALSE;
1223 static void
1224 calf_fader_allocate (GtkWidget *widget, GtkAllocation *allocation)
1226 calf_fader_set_layout(widget);
1228 static gboolean
1229 calf_fader_expose (GtkWidget *widget, GdkEventExpose *event)
1231 g_assert(CALF_IS_FADER(widget));
1232 if (gtk_widget_is_drawable (widget)) {
1234 GdkWindow *window = widget->window;
1235 GtkScale *scale = GTK_SCALE(widget);
1236 GtkRange *range = GTK_RANGE(widget);
1237 CalfFader *fader = CALF_FADER(widget);
1238 CalfFaderLayout layout = fader->layout;
1239 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
1241 cairo_rectangle(c, layout.x, layout.y, layout.w, layout.h);
1242 cairo_clip(c);
1244 // draw slit
1245 double r = range->adjustment->upper - range->adjustment->lower;
1246 double v0 = range->adjustment->value - range->adjustment->lower;
1247 if ((fader->horizontal and gtk_range_get_inverted(range))
1248 or (!fader->horizontal and gtk_range_get_inverted(range)))
1249 v0 = -v0 + 1;
1250 int vp = v0 / r * (fader->horizontal ? layout.w - layout.slw : layout.h - layout.slh)
1251 + (fader->horizontal ? layout.slw : layout.slh) / 2
1252 + (fader->horizontal ? layout.x : layout.y);
1253 layout.slx = fader->horizontal ? vp - layout.slw / 2 : layout.tc - layout.slw / 2;
1254 layout.sly = fader->horizontal ? layout.tc - layout.slh / 2 : vp - layout.slh / 2;
1256 cairo_rectangle(c, layout.sx - 1, layout.sy - 1, layout.sw, layout.sh);
1257 cairo_set_source_rgba(c, 0,0,0, 0.25);
1258 cairo_fill(c);
1259 cairo_rectangle(c, layout.sx + 1, layout.sy + 1, layout.sw, layout.sh);
1260 cairo_set_source_rgba(c, 1,1,1, 0.125);
1261 cairo_fill(c);
1262 cairo_rectangle(c, layout.sx, layout.sy, layout.sw, layout.sh);
1263 cairo_set_source_rgb(c, 0,0,0);
1264 cairo_fill(c);
1266 // draw screws
1267 cairo_rectangle(c, layout.x, layout.y, layout.w, layout.h);
1268 gdk_cairo_set_source_pixbuf(c, fader->screw, layout.scx1, layout.scy1);
1269 cairo_fill_preserve(c);
1270 gdk_cairo_set_source_pixbuf(c, fader->screw, layout.scx2, layout.scy2);
1271 cairo_fill_preserve(c);
1273 // draw slider
1274 if (fader->hover or gtk_grab_get_current() == widget)
1275 gdk_cairo_set_source_pixbuf(c, fader->sliderpre, layout.slx, layout.sly);
1276 else
1277 gdk_cairo_set_source_pixbuf(c, fader->slider, layout.slx, layout.sly);
1278 cairo_fill(c);
1280 // draw value label
1281 if (scale->draw_value) {
1282 PangoLayout *layout;
1283 gint _x, _y;
1284 layout = gtk_scale_get_layout (scale);
1285 gtk_scale_get_layout_offsets (scale, &_x, &_y);
1286 gtk_paint_layout (widget->style, window, GTK_STATE_NORMAL, FALSE, NULL,
1287 widget, fader->horizontal ? "hscale" : "vscale", _x, _y, layout);
1290 cairo_destroy(c);
1292 return FALSE;
1295 static void
1296 calf_fader_class_init (CalfFaderClass *klass)
1298 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1299 widget_class->expose_event = calf_fader_expose;
1302 static void
1303 calf_fader_init (CalfFader *self)
1305 GtkWidget *widget = GTK_WIDGET(self);
1306 widget->requisition.width = 40;
1307 widget->requisition.height = 40;
1309 gtk_signal_connect(GTK_OBJECT(widget), "motion-notify-event", GTK_SIGNAL_FUNC (calf_fader_motion), NULL);
1310 gtk_signal_connect(GTK_OBJECT(widget), "enter-notify-event", GTK_SIGNAL_FUNC (calf_fader_enter), NULL);
1311 gtk_signal_connect(GTK_OBJECT(widget), "leave-notify-event", GTK_SIGNAL_FUNC (calf_fader_leave), NULL);
1312 gtk_signal_connect(GTK_OBJECT(widget), "size-allocate", GTK_SIGNAL_FUNC (calf_fader_allocate), NULL);
1315 GType
1316 calf_fader_get_type (void)
1318 static GType type = 0;
1319 if (!type) {
1320 static const GTypeInfo type_info = {
1321 sizeof(CalfFaderClass),
1322 NULL, /* base_init */
1323 NULL, /* base_finalize */
1324 (GClassInitFunc)calf_fader_class_init,
1325 NULL, /* class_finalize */
1326 NULL, /* class_data */
1327 sizeof(CalfFader),
1328 0, /* n_preallocs */
1329 (GInstanceInitFunc)calf_fader_init
1332 for (int i = 0; ; i++) {
1333 char *name = g_strdup_printf("CalfFader%u%d",
1334 ((unsigned int)(intptr_t)calf_fader_class_init) >> 16, i);
1335 if (g_type_from_name(name)) {
1336 free(name);
1337 continue;
1339 type = g_type_register_static(GTK_TYPE_SCALE,
1340 name,
1341 &type_info,
1342 (GTypeFlags)0);
1343 free(name);
1344 break;
1347 return type;
1351 ///////////////////////////////////////// button ///////////////////////////////////////////////
1353 GtkWidget *
1354 calf_button_new(const gchar *label)
1356 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_BUTTON, NULL ));
1357 gtk_button_set_label(GTK_BUTTON(widget), label);
1358 return widget;
1360 static gboolean
1361 calf_button_expose (GtkWidget *widget, GdkEventExpose *event)
1363 g_assert(CALF_IS_BUTTON(widget) || CALF_IS_TOGGLE_BUTTON(widget) || CALF_IS_RADIO_BUTTON(widget));
1365 if (gtk_widget_is_drawable (widget)) {
1367 int pad = 2;
1369 GdkWindow *window = widget->window;
1370 GtkWidget *child = GTK_BIN (widget)->child;
1371 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
1372 cairo_pattern_t *pat = NULL;
1374 int x = widget->allocation.x;
1375 int y = widget->allocation.y;
1376 int sx = widget->allocation.width;
1377 int sy = widget->allocation.height;
1379 cairo_rectangle(c, x, y, sx, sy);
1380 cairo_clip(c);
1382 cairo_rectangle(c, x, y, sx, sy);
1383 pat = cairo_pattern_create_radial(x + sx / 2, y + sy / 2, 1, x + sx / 2, y + sy / 2, sx / 2);
1384 switch (gtk_widget_get_state(widget)) {
1385 case GTK_STATE_NORMAL:
1386 default:
1387 cairo_pattern_add_color_stop_rgb(pat, 0.3, 39. / 255, 52. / 255, 87. / 255);
1388 cairo_pattern_add_color_stop_rgb(pat, 1.0, 6. / 255, 5. / 255, 14. / 255);
1389 break;
1390 case GTK_STATE_PRELIGHT:
1391 cairo_pattern_add_color_stop_rgb(pat, 0.3, 19. / 255, 237. / 255, 254. / 255);
1392 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0. / 255, 45. / 255, 206. / 255);
1393 break;
1394 case GTK_STATE_ACTIVE:
1395 case GTK_STATE_SELECTED:
1396 cairo_pattern_add_color_stop_rgb(pat, 0.0, 19. / 255, 237. / 255, 254. / 255);
1397 cairo_pattern_add_color_stop_rgb(pat, 0.3, 10. / 255, 200. / 255, 240. / 255);
1398 cairo_pattern_add_color_stop_rgb(pat, 0.7, 19. / 255, 237. / 255, 254. / 255);
1399 cairo_pattern_add_color_stop_rgb(pat, 1.0, 2. / 255, 168. / 255, 230. / 255);
1400 break;
1403 cairo_set_source(c, pat);
1404 cairo_fill(c);
1406 cairo_rectangle(c, x + pad, y + pad, sx - pad * 2, sy - pad * 2);
1407 if (CALF_IS_TOGGLE_BUTTON(widget) or CALF_IS_RADIO_BUTTON(widget)) {
1408 cairo_new_sub_path (c);
1409 cairo_rectangle(c, x + sx - pad * 2 - 23, y + sy / 2 - 1, 22, 2);
1410 cairo_set_fill_rule(c, CAIRO_FILL_RULE_EVEN_ODD);
1412 pat = cairo_pattern_create_linear(x + pad, y + pad, x + pad, y + sy - pad * 2);
1413 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0.92, 0.92, 0.92);
1414 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0.70, 0.70, 0.70);
1415 cairo_set_source(c, pat);
1416 cairo_fill(c);
1418 int _h = GTK_WIDGET(GTK_BIN(widget)->child)->allocation.height + 0;
1419 int _y = y + (sy - _h) / 2;
1420 cairo_rectangle(c, x + pad, _y, sx - pad * 2, _h);
1421 if (CALF_IS_TOGGLE_BUTTON(widget) or CALF_IS_RADIO_BUTTON(widget)) {
1422 cairo_new_sub_path (c);
1423 cairo_rectangle(c, x + sx - pad * 2 - 23, y + sy / 2 - 1, 22, 2);
1424 cairo_set_fill_rule(c, CAIRO_FILL_RULE_EVEN_ODD);
1426 pat = cairo_pattern_create_linear(x + pad, _y, x + pad, _y + _h);
1427 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0.92, 0.92, 0.92);
1428 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0.70, 0.70, 0.70);
1429 cairo_set_source(c, pat);
1430 cairo_fill(c);
1432 cairo_destroy(c);
1433 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
1435 return FALSE;
1438 static void
1439 calf_button_class_init (CalfButtonClass *klass)
1441 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1442 widget_class->expose_event = calf_button_expose;
1445 static void
1446 calf_button_init (CalfButton *self)
1448 GtkWidget *widget = GTK_WIDGET(self);
1449 widget->requisition.width = 40;
1450 widget->requisition.height = 20;
1453 GType
1454 calf_button_get_type (void)
1456 static GType type = 0;
1457 if (!type) {
1458 static const GTypeInfo type_info = {
1459 sizeof(CalfButtonClass),
1460 NULL, /* base_init */
1461 NULL, /* base_finalize */
1462 (GClassInitFunc)calf_button_class_init,
1463 NULL, /* class_finalize */
1464 NULL, /* class_data */
1465 sizeof(CalfButton),
1466 0, /* n_preallocs */
1467 (GInstanceInitFunc)calf_button_init
1470 for (int i = 0; ; i++) {
1471 char *name = g_strdup_printf("CalfButton%u%d",
1472 ((unsigned int)(intptr_t)calf_button_class_init) >> 16, i);
1473 if (g_type_from_name(name)) {
1474 free(name);
1475 continue;
1477 type = g_type_register_static(GTK_TYPE_BUTTON,
1478 name,
1479 &type_info,
1480 (GTypeFlags)0);
1481 free(name);
1482 break;
1485 return type;
1489 ///////////////////////////////////////// toggle button ///////////////////////////////////////////////
1491 GtkWidget *
1492 calf_toggle_button_new(const gchar *label)
1494 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_TOGGLE_BUTTON, NULL ));
1495 gtk_button_set_label(GTK_BUTTON(widget), label);
1496 return widget;
1499 static void
1500 calf_toggle_button_class_init (CalfToggleButtonClass *klass)
1502 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1503 widget_class->expose_event = calf_button_expose;
1506 static void
1507 calf_toggle_button_init (CalfToggleButton *self)
1509 GtkWidget *widget = GTK_WIDGET(self);
1510 widget->requisition.width = 40;
1511 widget->requisition.height = 20;
1514 GType
1515 calf_toggle_button_get_type (void)
1517 static GType type = 0;
1518 if (!type) {
1519 static const GTypeInfo type_info = {
1520 sizeof(CalfToggleButtonClass),
1521 NULL, /* base_init */
1522 NULL, /* base_finalize */
1523 (GClassInitFunc)calf_toggle_button_class_init,
1524 NULL, /* class_finalize */
1525 NULL, /* class_data */
1526 sizeof(CalfToggleButton),
1527 0, /* n_preallocs */
1528 (GInstanceInitFunc)calf_toggle_button_init
1531 for (int i = 0; ; i++) {
1532 char *name = g_strdup_printf("CalfToggleButton%u%d",
1533 ((unsigned int)(intptr_t)calf_toggle_button_class_init) >> 16, i);
1534 if (g_type_from_name(name)) {
1535 free(name);
1536 continue;
1538 type = g_type_register_static(GTK_TYPE_TOGGLE_BUTTON,
1539 name,
1540 &type_info,
1541 (GTypeFlags)0);
1542 free(name);
1543 break;
1546 return type;
1549 ///////////////////////////////////////// radio button ///////////////////////////////////////////////
1551 GtkWidget *
1552 calf_radio_button_new(const gchar *label)
1554 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_RADIO_BUTTON, NULL ));
1555 gtk_button_set_label(GTK_BUTTON(widget), label);
1556 return widget;
1559 static void
1560 calf_radio_button_class_init (CalfRadioButtonClass *klass)
1562 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1563 widget_class->expose_event = calf_button_expose;
1566 static void
1567 calf_radio_button_init (CalfRadioButton *self)
1569 GtkWidget *widget = GTK_WIDGET(self);
1570 widget->requisition.width = 40;
1571 widget->requisition.height = 20;
1574 GType
1575 calf_radio_button_get_type (void)
1577 static GType type = 0;
1578 if (!type) {
1579 static const GTypeInfo type_info = {
1580 sizeof(CalfRadioButtonClass),
1581 NULL, /* base_init */
1582 NULL, /* base_finalize */
1583 (GClassInitFunc)calf_radio_button_class_init,
1584 NULL, /* class_finalize */
1585 NULL, /* class_data */
1586 sizeof(CalfRadioButton),
1587 0, /* n_preallocs */
1588 (GInstanceInitFunc)calf_radio_button_init
1591 for (int i = 0; ; i++) {
1592 char *name = g_strdup_printf("CalfRadioButton%u%d",
1593 ((unsigned int)(intptr_t)calf_radio_button_class_init) >> 16, i);
1594 if (g_type_from_name(name)) {
1595 free(name);
1596 continue;
1598 type = g_type_register_static(GTK_TYPE_RADIO_BUTTON,
1599 name,
1600 &type_info,
1601 (GTypeFlags)0);
1602 free(name);
1603 break;
1606 return type;
1609 ///////////////////////////////////////// tap button ///////////////////////////////////////////////
1611 GtkWidget *
1612 calf_tap_button_new()
1614 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_TAP_BUTTON, NULL ));
1615 return widget;
1618 static gboolean
1619 calf_tap_button_expose (GtkWidget *widget, GdkEventExpose *event)
1621 g_assert(CALF_IS_TAP_BUTTON(widget));
1622 CalfTapButton *self = CALF_TAP_BUTTON(widget);
1624 int x = widget->allocation.x + widget->allocation.width / 2 - 35;
1625 int y = widget->allocation.y + widget->allocation.height / 2 - 35;
1626 int width = 70;
1627 int height = 70;
1629 gdk_draw_pixbuf(GDK_DRAWABLE(widget->window),
1630 widget->style->fg_gc[0],
1631 self->image[self->state],
1636 width,
1637 height,
1638 GDK_RGB_DITHER_NORMAL, 0, 0);
1639 return TRUE;
1642 static void
1643 calf_tap_button_size_request (GtkWidget *widget,
1644 GtkRequisition *requisition)
1646 g_assert(CALF_IS_TAP_BUTTON(widget));
1647 requisition->width = 70;
1648 requisition->height = 70;
1650 static void
1651 calf_tap_button_class_init (CalfTapButtonClass *klass)
1653 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
1654 widget_class->expose_event = calf_tap_button_expose;
1655 widget_class->size_request = calf_tap_button_size_request;
1657 static void
1658 calf_tap_button_init (CalfTapButton *self)
1660 GtkWidget *widget = GTK_WIDGET(self);
1661 widget->requisition.width = 70;
1662 widget->requisition.height = 70;
1663 self->state = 0;
1664 GError *error = NULL;
1665 self->image[0] = gdk_pixbuf_new_from_file(PKGLIBDIR "/tap_inactive.png", &error);
1666 self->image[1] = gdk_pixbuf_new_from_file(PKGLIBDIR "/tap_prelight.png", &error);
1667 self->image[2] = gdk_pixbuf_new_from_file(PKGLIBDIR "/tap_active.png", &error);
1670 GType
1671 calf_tap_button_get_type (void)
1673 static GType type = 0;
1674 if (!type) {
1675 static const GTypeInfo type_info = {
1676 sizeof(CalfTapButtonClass),
1677 NULL, /* base_init */
1678 NULL, /* base_finalize */
1679 (GClassInitFunc)calf_tap_button_class_init,
1680 NULL, /* class_finalize */
1681 NULL, /* class_data */
1682 sizeof(CalfTapButton),
1683 0, /* n_preallocs */
1684 (GInstanceInitFunc)calf_tap_button_init
1687 for (int i = 0; ; i++) {
1688 char *name = g_strdup_printf("CalfTapButton%u%d",
1689 ((unsigned int)(intptr_t)calf_tap_button_class_init) >> 16, i);
1690 if (g_type_from_name(name)) {
1691 free(name);
1692 continue;
1694 type = g_type_register_static(GTK_TYPE_BUTTON,
1695 name,
1696 &type_info,
1697 (GTypeFlags)0);
1698 free(name);
1699 break;
1702 return type;