2 * Custom controls (line graph, knob).
3 * Copyright (C) 2007 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <calf/custom_ctl.h>
22 #include <gdk/gdkkeysyms.h>
23 #include <cairo/cairo.h>
28 I don't really know how to do it, or if it can be done this way.
29 struct calf_ui_type_module
35 module = g_type_module_new();
36 g_type_module_set_name(module, "calf_custom_ctl");
37 g_type_module_use(module);
39 ~calf_ui_type_module()
41 g_type_module_unuse(module);
45 static calf_ui_type_module type_module;
49 calf_line_graph_copy_cache_to_window( CalfLineGraph
*lg
, cairo_t
*c
)
52 cairo_set_source_surface( c
, lg
->cache_surface
, 0,0 );
58 calf_line_graph_copy_window_to_cache( CalfLineGraph
*lg
, cairo_t
*c
)
60 cairo_t
*cache_cr
= cairo_create( lg
->cache_surface
);
61 cairo_surface_t
*window_surface
= cairo_get_target( c
);
62 cairo_set_source_surface( cache_cr
, window_surface
, 0,0 );
63 cairo_paint( cache_cr
);
64 cairo_destroy( cache_cr
);
68 calf_line_graph_draw_grid( cairo_t
*c
, std::string
&legend
, bool vertical
, float pos
, int phase
, int sx
, int sy
)
71 cairo_text_extents_t tx
;
73 cairo_text_extents(c
, legend
.c_str(), &tx
);
76 float x
= floor(ox
+ pos
* sx
) + 0.5;
79 cairo_move_to(c
, x
, oy
);
80 cairo_line_to(c
, x
, oy
+ sy
);
83 if (phase
== 2 && !legend
.empty()) {
85 cairo_set_source_rgba(c
, 1.0, 1.0, 1.0, 0.75);
86 cairo_move_to(c
, x
- (tx
.x_bearing
+ tx
.width
/ 2.0), oy
+ sy
- 2);
87 cairo_show_text(c
, legend
.c_str());
92 float y
= floor(oy
+ sy
/ 2 - (sy
/ 2 - 1) * pos
) + 0.5;
95 cairo_move_to(c
, ox
, y
);
96 cairo_line_to(c
, ox
+ sx
, y
);
99 if (phase
== 2 && !legend
.empty()) {
100 cairo_set_source_rgba(c
, 1.0, 1.0, 1.0, 0.75);
101 cairo_move_to(c
, ox
+ sx
- 2 - tx
.width
, y
+ tx
.height
/2 - 1);
102 cairo_show_text(c
, legend
.c_str());
108 calf_line_graph_draw_graph( cairo_t
*c
, float *data
, int sx
, int sy
)
112 for (int i
= 0; i
< 2 * sx
; i
++)
114 int y
= (int)(oy
+ sy
/ 2 - (sy
/ 2 - 1) * data
[i
]);
115 //if (y < oy) y = oy;
116 //if (y >= oy + sy) y = oy + sy - 1;
118 cairo_line_to(c
, ox
+ i
* 0.5, y
);
120 cairo_move_to(c
, ox
, y
);
126 calf_line_graph_expose (GtkWidget
*widget
, GdkEventExpose
*event
)
128 g_assert(CALF_IS_LINE_GRAPH(widget
));
130 CalfLineGraph
*lg
= CALF_LINE_GRAPH(widget
);
131 //int ox = widget->allocation.x + 1, oy = widget->allocation.y + 1;
133 int sx
= widget
->allocation
.width
- 2, sy
= widget
->allocation
.height
- 2;
135 cairo_t
*c
= gdk_cairo_create(GDK_DRAWABLE(widget
->window
));
137 GdkColor sc
= { 0, 0, 0, 0 };
139 bool cache_dirty
= 0;
143 if( lg
->cache_surface
== NULL
) {
144 // looks like its either first call or the widget has been resized.
145 // create the cache_surface.
146 cairo_surface_t
*window_surface
= cairo_get_target( c
);
147 lg
->cache_surface
= cairo_surface_create_similar( window_surface
,
149 widget
->allocation
.width
,
150 widget
->allocation
.height
);
151 //cairo_set_source_surface( cache_cr, window_surface, 0,0 );
152 //cairo_paint( cache_cr );
157 gdk_cairo_set_source_color(c
, &sc
);
158 cairo_rectangle(c
, ox
, oy
, sx
, sy
);
159 cairo_clip_preserve(c
);
163 cairo_select_font_face(c
, "Bitstream Vera Sans", CAIRO_FONT_SLANT_NORMAL
, CAIRO_FONT_WEIGHT_NORMAL
);
164 cairo_set_font_size(c
, 9);
169 bool vertical
= false;
171 float *data
= new float[2 * sx
];
172 GdkColor sc2
= { 0, 0, 65535, 0 };
175 GdkColor sc3
= { 0, 32767, 65535, 0 };
177 int graph_n
, grid_n
, dot_n
, grid_n_save
;
179 int cache_graph_index
, cache_dot_index
, cache_grid_index
;
180 int gen_index
= lg
->source
->get_changed_offsets( lg
->last_generation
, cache_graph_index
, cache_dot_index
, cache_grid_index
);
182 if( cache_dirty
|| (gen_index
!= lg
->last_generation
) ) {
183 lg
->source
->get_changed_offsets( gen_index
, cache_graph_index
, cache_dot_index
, cache_grid_index
);
184 lg
->last_generation
= gen_index
;
186 printf( "oki... cache_graph_index = %d\n", cache_graph_index
);
188 cairo_set_line_width(c
, 1);
189 for(int phase
= 1; phase
<= 2; phase
++)
191 for(grid_n
= 0; legend
= std::string(), cairo_set_source_rgba(c
, 1, 1, 1, 0.5), (grid_n
<cache_grid_index
) && lg
->source
->get_gridline(lg
->source_id
, grid_n
, pos
, vertical
, legend
, &cimpl
); grid_n
++)
193 printf( "draw gridline %d\n", grid_n
);
194 calf_line_graph_draw_grid( c
, legend
, vertical
, pos
, phase
, sx
, sy
);
197 grid_n_save
= grid_n
-1;
199 gdk_cairo_set_source_color(c
, &sc2
);
200 cairo_set_line_join(c
, CAIRO_LINE_JOIN_MITER
);
201 cairo_set_line_width(c
, 1);
202 for(graph_n
= 0; (graph_n
<cache_graph_index
) && lg
->source
->get_graph(lg
->source_id
, graph_n
, data
, 2 * sx
, &cimpl
); graph_n
++)
204 calf_line_graph_draw_graph( c
, data
, sx
, sy
);
206 gdk_cairo_set_source_color(c
, &sc3
);
207 for(dot_n
= 0; (dot_n
<cache_dot_index
) && lg
->source
->get_dot(lg
->source_id
, dot_n
, x
, y
, size
= 3, &cimpl
); dot_n
++)
209 int yv
= (int)(oy
+ sy
/ 2 - (sy
/ 2 - 1) * y
);
210 cairo_arc(c
, ox
+ x
* sx
, yv
, size
, 0, 2 * M_PI
);
214 // copy window to cache.
215 calf_line_graph_copy_window_to_cache( lg
, c
);
217 grid_n_save
= cache_grid_index
;
218 graph_n
= cache_graph_index
;
219 dot_n
= cache_dot_index
;
220 calf_line_graph_copy_cache_to_window( lg
, c
);
222 printf( "cache paint ... cache_graph_index = %d\n", cache_graph_index
);
226 cairo_set_line_width(c
, 1);
227 for(int phase
= 1; phase
<= 2; phase
++)
229 for(int gn
=grid_n_save
; legend
= std::string(), cairo_set_source_rgba(c
, 1, 1, 1, 0.5), lg
->source
->get_gridline(lg
->source_id
, gn
, pos
, vertical
, legend
, &cimpl
); gn
++)
231 printf( "after cache:draw gridline %d\n", gn
);
232 calf_line_graph_draw_grid( c
, legend
, vertical
, pos
, phase
, sx
, sy
);
236 gdk_cairo_set_source_color(c
, &sc2
);
237 cairo_set_line_join(c
, CAIRO_LINE_JOIN_MITER
);
238 cairo_set_line_width(c
, 1);
239 for(int gn
= graph_n
; lg
->source
->get_graph(lg
->source_id
, gn
, data
, 2 * sx
, &cimpl
); gn
++)
241 //printf( "uncached graph draw = %d\n", gn );
242 calf_line_graph_draw_graph( c
, data
, sx
, sy
);
244 gdk_cairo_set_source_color(c
, &sc3
);
245 for(int gn
= dot_n
; lg
->source
->get_dot(lg
->source_id
, gn
, x
, y
, size
= 3, &cimpl
); gn
++)
247 int yv
= (int)(oy
+ sy
/ 2 - (sy
/ 2 - 1) * y
);
248 cairo_arc(c
, ox
+ x
* sx
, yv
, size
, 0, 2 * M_PI
);
256 gtk_paint_shadow(widget
->style
, widget
->window
, GTK_STATE_NORMAL
, GTK_SHADOW_IN
, NULL
, widget
, NULL
, ox
- 1, oy
- 1, sx
+ 2, sy
+ 2);
257 // printf("exposed %p %d+%d\n", widget->window, widget->allocation.x, widget->allocation.y);
262 void calf_line_graph_set_square(CalfLineGraph
*graph
, bool is_square
)
264 g_assert(CALF_IS_LINE_GRAPH(graph
));
265 graph
->is_square
= is_square
;
269 calf_line_graph_size_request (GtkWidget
*widget
,
270 GtkRequisition
*requisition
)
272 g_assert(CALF_IS_LINE_GRAPH(widget
));
274 // CalfLineGraph *lg = CALF_LINE_GRAPH(widget);
275 requisition
->width
= 40;
276 requisition
->height
= 40;
280 calf_line_graph_size_allocate (GtkWidget
*widget
,
281 GtkAllocation
*allocation
)
283 g_assert(CALF_IS_LINE_GRAPH(widget
));
284 CalfLineGraph
*lg
= CALF_LINE_GRAPH(widget
);
286 GtkWidgetClass
*parent_class
= (GtkWidgetClass
*) g_type_class_peek_parent( CALF_LINE_GRAPH_GET_CLASS( lg
) );
288 if( lg
->cache_surface
)
289 cairo_surface_destroy( lg
->cache_surface
);
290 lg
->cache_surface
= NULL
;
292 widget
->allocation
= *allocation
;
293 GtkAllocation
&a
= widget
->allocation
;
296 if (a
.width
> a
.height
)
298 a
.x
+= (a
.width
- a
.height
) / 2;
301 if (a
.width
< a
.height
)
303 a
.y
+= (a
.height
- a
.width
) / 2;
307 parent_class
->size_allocate( widget
, allocation
);
311 calf_line_graph_class_init (CalfLineGraphClass
*klass
)
313 // GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
314 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS(klass
);
315 widget_class
->expose_event
= calf_line_graph_expose
;
316 widget_class
->size_request
= calf_line_graph_size_request
;
317 widget_class
->size_allocate
= calf_line_graph_size_allocate
;
321 calf_line_graph_init (CalfLineGraph
*self
)
323 GtkWidget
*widget
= GTK_WIDGET(self
);
324 //GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
325 //widget->requisition.width = 40;
326 //widget->requisition.height = 40;
327 gtk_widget_set_size_request( widget
, 40, 40 );
328 self
->cache_surface
= NULL
;
332 calf_line_graph_new()
334 return GTK_WIDGET( g_object_new (CALF_TYPE_LINE_GRAPH
, NULL
));
338 calf_line_graph_get_type (void)
340 static GType type
= 0;
342 static const GTypeInfo type_info
= {
343 sizeof(CalfLineGraphClass
),
344 NULL
, /* base_init */
345 NULL
, /* base_finalize */
346 (GClassInitFunc
)calf_line_graph_class_init
,
347 NULL
, /* class_finalize */
348 NULL
, /* class_data */
349 sizeof(CalfLineGraph
),
351 (GInstanceInitFunc
)calf_line_graph_init
354 GTypeInfo
*type_info_copy
= new GTypeInfo(type_info
);
356 for (int i
= 0; ; i
++) {
357 char *name
= g_strdup_printf("CalfLineGraph%u%d", ((unsigned int)(intptr_t)calf_line_graph_class_init
) >> 16, i
);
358 if (g_type_from_name(name
)) {
362 type
= g_type_register_static( GTK_TYPE_DRAWING_AREA
,
373 ///////////////////////////////////////// vu meter ///////////////////////////////////////////////
376 calf_vumeter_expose (GtkWidget
*widget
, GdkEventExpose
*event
)
378 g_assert(CALF_IS_VUMETER(widget
));
381 CalfVUMeter
*vu
= CALF_VUMETER(widget
);
382 //int ox = widget->allocation.x + 1, oy = widget->allocation.y + 1;
384 int sx
= widget
->allocation
.width
- 2, sy
= widget
->allocation
.height
- 2;
386 cairo_t
*c
= gdk_cairo_create(GDK_DRAWABLE(widget
->window
));
388 GdkColor sc
= { 0, 0, 0, 0 };
389 gdk_cairo_set_source_color(c
, &sc
);
390 cairo_rectangle(c
, ox
, oy
, sx
, sy
);
392 cairo_set_line_width(c
, 1);
394 CalfVUMeterMode mode
= vu
->mode
;
396 for (int x
= ox
; x
<= ox
+ sx
; x
+= 3)
398 float ts
= (x
- ox
) * 1.0 / sx
;
399 float r
= 0.f
, g
= 0.f
, b
= 0.f
;
405 r
= ts
/ 0.75, g
= 1, b
= 0;
407 r
= 1, g
= 1 - (ts
- 0.75) / 0.25, b
= 0;
408 if (vu
->value
< ts
|| vu
->value
<= 0)
409 r
*= 0.5, g
*= 0.5, b
*= 0.5;
411 case VU_MONOCHROME_REVERSE
:
413 if (!(vu
->value
< ts
) || vu
->value
>= 1.0)
414 r
*= 0.5, g
*= 0.5, b
*= 0.5;
418 if (vu
->value
< ts
|| vu
->value
<= 0)
419 r
*= 0.5, g
*= 0.5, b
*= 0.5;
422 GdkColor sc2
= { 0, (guint16
)(65535 * r
), (guint16
)(65535 * g
), (guint16
)(65535 * b
) };
423 gdk_cairo_set_source_color(c
, &sc2
);
424 cairo_move_to(c
, x
, oy
);
425 cairo_line_to(c
, x
, oy
+ sy
+ 1);
426 cairo_move_to(c
, x
, oy
+ sy
);
427 cairo_line_to(c
, x
, oy
);
433 gtk_paint_shadow(widget
->style
, widget
->window
, GTK_STATE_NORMAL
, GTK_SHADOW_IN
, NULL
, widget
, NULL
, ox
- 1, oy
- 1, sx
+ 2, sy
+ 2);
434 printf("exposed %p %d+%d\n", widget
->window
, widget
->allocation
.x
, widget
->allocation
.y
);
440 calf_vumeter_size_request (GtkWidget
*widget
,
441 GtkRequisition
*requisition
)
443 g_assert(CALF_IS_VUMETER(widget
));
445 requisition
->width
= 50;
446 requisition
->height
= 14;
450 calf_vumeter_size_allocate (GtkWidget
*widget
,
451 GtkAllocation
*allocation
)
453 g_assert(CALF_IS_VUMETER(widget
));
455 widget
->allocation
= *allocation
;
459 calf_vumeter_class_init (CalfVUMeterClass
*klass
)
461 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS(klass
);
462 widget_class
->expose_event
= calf_vumeter_expose
;
463 widget_class
->size_request
= calf_vumeter_size_request
;
464 //widget_class->size_allocate = calf_vumeter_size_allocate;
468 calf_vumeter_init (CalfVUMeter
*self
)
470 GtkWidget
*widget
= GTK_WIDGET(self
);
471 //GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
472 widget
->requisition
.width
= 50;
473 widget
->requisition
.height
= 15;
480 return GTK_WIDGET( g_object_new (CALF_TYPE_VUMETER
, NULL
));
484 calf_vumeter_get_type (void)
486 static GType type
= 0;
488 static const GTypeInfo type_info
= {
489 sizeof(CalfVUMeterClass
),
490 NULL
, /* base_init */
491 NULL
, /* base_finalize */
492 (GClassInitFunc
)calf_vumeter_class_init
,
493 NULL
, /* class_finalize */
494 NULL
, /* class_data */
497 (GInstanceInitFunc
)calf_vumeter_init
500 GTypeInfo
*type_info_copy
= new GTypeInfo(type_info
);
502 for (int i
= 0; ; i
++) {
503 char *name
= g_strdup_printf("CalfVUMeter%u%d", ((unsigned int)(intptr_t)calf_vumeter_class_init
) >> 16, i
);
504 if (g_type_from_name(name
)) {
508 type
= g_type_register_static( GTK_TYPE_DRAWING_AREA
,
519 extern void calf_vumeter_set_value(CalfVUMeter
*meter
, float value
)
521 if (value
!= meter
->value
)
523 meter
->value
= value
;
524 gtk_widget_queue_draw(GTK_WIDGET(meter
));
528 extern float calf_vumeter_get_value(CalfVUMeter
*meter
)
533 extern void calf_vumeter_set_mode(CalfVUMeter
*meter
, CalfVUMeterMode mode
)
535 if (mode
!= meter
->mode
)
538 gtk_widget_queue_draw(GTK_WIDGET(meter
));
542 extern CalfVUMeterMode
calf_vumeter_get_mode(CalfVUMeter
*meter
)
547 ///////////////////////////////////////// knob ///////////////////////////////////////////////
550 calf_knob_expose (GtkWidget
*widget
, GdkEventExpose
*event
)
552 g_assert(CALF_IS_KNOB(widget
));
554 CalfKnob
*self
= CALF_KNOB(widget
);
555 GdkWindow
*window
= widget
->window
;
556 GtkAdjustment
*adj
= gtk_range_get_adjustment(GTK_RANGE(widget
));
558 // printf("adjustment = %p value = %f\n", adj, adj->value);
559 int ox
= widget
->allocation
.x
, oy
= widget
->allocation
.y
;
561 ox
+= (widget
->allocation
.width
- 40) / 2;
562 oy
+= (widget
->allocation
.height
- 40) / 2;
564 int phase
= (int)((adj
->value
- adj
->lower
) * 64 / (adj
->upper
- adj
->lower
));
565 // skip middle phase except for true middle value
566 if (self
->knob_type
== 1 && phase
== 32) {
567 double pt
= (adj
->value
- adj
->lower
) * 2.0 / (adj
->upper
- adj
->lower
) - 1.0;
573 // endless knob: skip 90deg highlights unless the value is really a multiple of 90deg
574 if (self
->knob_type
== 3 && !(phase
% 16)) {
577 double nom
= adj
->lower
+ phase
* (adj
->upper
- adj
->lower
) / 64.0;
578 double diff
= (adj
->value
- nom
) / (adj
->upper
- adj
->lower
);
580 phase
= (phase
+ 1) % 64;
582 phase
= (phase
+ 63) % 64;
584 gdk_draw_pixbuf(GDK_DRAWABLE(widget
->window
), widget
->style
->fg_gc
[0], CALF_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget
))->knob_image
, phase
* 40, self
->knob_type
* 40, ox
, oy
, 40, 40, GDK_RGB_DITHER_NORMAL
, 0, 0);
585 // printf("exposed %p %d+%d\n", widget->window, widget->allocation.x, widget->allocation.y);
586 if (gtk_widget_is_focus(widget
))
588 gtk_paint_focus(widget
->style
, window
, GTK_STATE_NORMAL
, NULL
, widget
, NULL
, ox
, oy
, 40, 40);
595 calf_knob_size_request (GtkWidget
*widget
,
596 GtkRequisition
*requisition
)
598 g_assert(CALF_IS_KNOB(widget
));
600 // width/height is hardwired at 40px now
601 requisition
->width
= 40;
602 requisition
->height
= 40;
606 calf_knob_incr (GtkWidget
*widget
, int dir_down
)
608 g_assert(CALF_IS_KNOB(widget
));
609 CalfKnob
*self
= CALF_KNOB(widget
);
610 GtkAdjustment
*adj
= gtk_range_get_adjustment(GTK_RANGE(widget
));
612 int oldstep
= (int)(0.5f
+ (adj
->value
- adj
->lower
) / adj
->step_increment
);
614 int nsteps
= (int)(0.5f
+ (adj
->upper
- adj
->lower
) / adj
->step_increment
); // less 1 actually
619 if (self
->knob_type
== 3 && step
>= nsteps
)
621 if (self
->knob_type
== 3 && step
< 0)
622 step
= nsteps
- (nsteps
- step
) % nsteps
;
624 // trying to reduce error cumulation here, by counting from lowest or from highest
625 float value
= adj
->lower
+ step
* double(adj
->upper
- adj
->lower
) / nsteps
;
626 gtk_range_set_value(GTK_RANGE(widget
), value
);
627 // printf("step %d:%d nsteps %d value %f:%f\n", oldstep, step, nsteps, oldvalue, value);
631 calf_knob_key_press (GtkWidget
*widget
, GdkEventKey
*event
)
633 g_assert(CALF_IS_KNOB(widget
));
634 CalfKnob
*self
= CALF_KNOB(widget
);
635 GtkAdjustment
*adj
= gtk_range_get_adjustment(GTK_RANGE(widget
));
637 switch(event
->keyval
)
640 gtk_range_set_value(GTK_RANGE(widget
), adj
->lower
);
644 gtk_range_set_value(GTK_RANGE(widget
), adj
->upper
);
648 calf_knob_incr(widget
, 0);
652 calf_knob_incr(widget
, 1);
657 self
->start_value
= gtk_range_get_value(GTK_RANGE(widget
));
658 self
->start_y
= self
->last_y
;
666 calf_knob_key_release (GtkWidget
*widget
, GdkEventKey
*event
)
668 g_assert(CALF_IS_KNOB(widget
));
669 CalfKnob
*self
= CALF_KNOB(widget
);
671 if(event
->keyval
== GDK_Shift_L
|| event
->keyval
== GDK_Shift_R
)
673 self
->start_value
= gtk_range_get_value(GTK_RANGE(widget
));
674 self
->start_y
= self
->last_y
;
682 calf_knob_button_press (GtkWidget
*widget
, GdkEventButton
*event
)
684 g_assert(CALF_IS_KNOB(widget
));
685 CalfKnob
*self
= CALF_KNOB(widget
);
687 // CalfKnob *lg = CALF_KNOB(widget);
688 gtk_widget_grab_focus(widget
);
689 gtk_grab_add(widget
);
690 self
->start_x
= event
->x
;
691 self
->start_y
= event
->y
;
692 self
->start_value
= gtk_range_get_value(GTK_RANGE(widget
));
698 calf_knob_button_release (GtkWidget
*widget
, GdkEventButton
*event
)
700 g_assert(CALF_IS_KNOB(widget
));
702 if (GTK_WIDGET_HAS_GRAB(widget
))
703 gtk_grab_remove(widget
);
707 static inline float endless(float value
)
710 return fmod(value
, 1.f
);
712 return fmod(1.f
- fmod(1.f
- value
, 1.f
), 1.f
);
715 static inline float deadzone(float value
, float incr
, float scale
)
717 float dzw
= 10 / scale
;
725 if (value
>= (0.5 - dzw
) && value
<= (0.5 + dzw
))
733 calf_knob_pointer_motion (GtkWidget
*widget
, GdkEventMotion
*event
)
735 g_assert(CALF_IS_KNOB(widget
));
736 CalfKnob
*self
= CALF_KNOB(widget
);
738 float scale
= (event
->state
& GDK_SHIFT_MASK
) ? 1000 : 100;
739 gboolean moved
= FALSE
;
741 if (GTK_WIDGET_HAS_GRAB(widget
))
743 if (self
->knob_type
== 3)
745 gtk_range_set_value(GTK_RANGE(widget
), endless(self
->start_value
- (event
->y
- self
->start_y
) / scale
));
748 if (self
->knob_type
== 1)
750 gtk_range_set_value(GTK_RANGE(widget
), deadzone(self
->start_value
, -(event
->y
- self
->start_y
) / scale
, scale
));
754 gtk_range_set_value(GTK_RANGE(widget
), self
->start_value
- (event
->y
- self
->start_y
) / scale
);
758 self
->last_y
= event
->y
;
763 calf_knob_scroll (GtkWidget
*widget
, GdkEventScroll
*event
)
765 calf_knob_incr(widget
, event
->direction
);
770 calf_knob_class_init (CalfKnobClass
*klass
)
772 // GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
773 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS(klass
);
774 widget_class
->expose_event
= calf_knob_expose
;
775 widget_class
->size_request
= calf_knob_size_request
;
776 widget_class
->button_press_event
= calf_knob_button_press
;
777 widget_class
->button_release_event
= calf_knob_button_release
;
778 widget_class
->motion_notify_event
= calf_knob_pointer_motion
;
779 widget_class
->key_press_event
= calf_knob_key_press
;
780 widget_class
->key_release_event
= calf_knob_key_release
;
781 widget_class
->scroll_event
= calf_knob_scroll
;
782 GError
*error
= NULL
;
783 klass
->knob_image
= gdk_pixbuf_new_from_file(PKGLIBDIR
"/knob.png", &error
);
784 g_assert(klass
->knob_image
!= NULL
);
788 calf_knob_init (CalfKnob
*self
)
790 GtkWidget
*widget
= GTK_WIDGET(self
);
791 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(self
), GTK_CAN_FOCUS
);
792 widget
->requisition
.width
= 40;
793 widget
->requisition
.height
= 40;
799 GtkAdjustment
*adj
= (GtkAdjustment
*)gtk_adjustment_new(0, 0, 1, 0.01, 0.5, 0);
800 return calf_knob_new_with_adjustment(adj
);
803 static gboolean
calf_knob_value_changed(gpointer obj
)
805 GtkWidget
*widget
= (GtkWidget
*)obj
;
806 gtk_widget_queue_draw(widget
);
810 GtkWidget
*calf_knob_new_with_adjustment(GtkAdjustment
*_adjustment
)
812 GtkWidget
*widget
= GTK_WIDGET( g_object_new (CALF_TYPE_KNOB
, NULL
));
814 gtk_range_set_adjustment(GTK_RANGE(widget
), _adjustment
);
815 gtk_signal_connect(GTK_OBJECT(widget
), "value-changed", G_CALLBACK(calf_knob_value_changed
), widget
);
821 calf_knob_get_type (void)
823 static GType type
= 0;
826 static const GTypeInfo type_info
= {
827 sizeof(CalfKnobClass
),
828 NULL
, /* base_init */
829 NULL
, /* base_finalize */
830 (GClassInitFunc
)calf_knob_class_init
,
831 NULL
, /* class_finalize */
832 NULL
, /* class_data */
835 (GInstanceInitFunc
)calf_knob_init
838 for (int i
= 0; ; i
++) {
839 char *name
= g_strdup_printf("CalfKnob%u%d",
840 ((unsigned int)(intptr_t)calf_knob_class_init
) >> 16, i
);
841 if (g_type_from_name(name
)) {
845 type
= g_type_register_static(GTK_TYPE_RANGE
,