libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xknob_private.c
Go to the documentation of this file.
1 /*
2  * 0BSD
3  *
4  * BSD Zero Clause License
5  *
6  * Copyright (c) 2019 Hermann Meyer
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted.
10 
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 
22 #include "xknob_private.h"
23 
24 static void _show_label(Widget_t *w, int width, int height) {
26  cairo_text_extents_t extents;
27  /** show label below the knob**/
28  cairo_set_font_size (w->crb, w->app->normal_font/w->scale.ascale);
29  cairo_text_extents(w->crb,w->label , &extents);
30  cairo_move_to (w->crb, (width*0.5)-(extents.width/2), height-(extents.height/4));
31  cairo_show_text(w->crb, w->label);
32  cairo_new_path (w->crb);
33 }
34 
35 void _draw_image_knob(Widget_t *w, int width_t, int height_t) {
36  int width = cairo_xlib_surface_get_width(w->image);
37  int height = cairo_xlib_surface_get_height(w->image);
38  double x = (double)width_t/(double)height;
39  double y = (double)height/(double)width_t;
40  double knobstate = adj_get_state(w->adj_y);
41  int findex = (int)(((width/height)-1) * knobstate);
42  cairo_scale(w->crb, x,x);
43  //widget_set_scale(w);
44  cairo_set_source_surface (w->crb, w->image, -height*findex, 0);
45  cairo_rectangle(w->crb,0, 0, height, height);
46  cairo_fill(w->crb);
47  //widget_reset_scale(w);
48  cairo_scale(w->crb, y,y);
49 }
50 
51 void _draw_knob_image(void *w_, void* user_data) {
52  Widget_t *w = (Widget_t*)w_;
53  _draw_image_knob(w, w->width, w->height);
54  _show_label(w, w->width-2, w->height-2);
55 }
56 
57 void _draw_knob(void *w_, void* user_data) {
58  Widget_t *w = (Widget_t*)w_;
59  XWindowAttributes attrs;
60  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
61  int width = attrs.width-2;
62  int height = attrs.height-2;
63 
64  const double scale_zero = 20 * (M_PI/180); // defines "dead zone" for knobs
65  int arc_offset = 0;
66  int knob_x = 0;
67  int knob_y = 0;
68 
69  int grow = (width > height) ? height:width;
70  knob_x = grow-1;
71  knob_y = grow-1;
72  /** get values for the knob **/
73 
74  int knobx = (width - knob_x) * 0.5;
75  int knobx1 = width* 0.5;
76 
77  int knoby = (height - knob_y) * 0.5;
78  int knoby1 = height * 0.5;
79  if (w->image) {
80  _draw_image_knob(w, width, height);
81  } else {
82 
83  double knobstate = adj_get_state(w->adj_y);
84  double angle = scale_zero + knobstate * 2 * (M_PI - scale_zero);
85 
86  double pointer_off =knob_x/6;
87  double radius = min(knob_x-pointer_off, knob_y-pointer_off) / 2;
88  double lengh_x = (knobx+radius+pointer_off/2) - radius * sin(angle);
89  double lengh_y = (knoby+radius+pointer_off/2) + radius * cos(angle);
90  double radius_x = (knobx+radius+pointer_off/2) - radius/ 1.18 * sin(angle);
91  double radius_y = (knoby+radius+pointer_off/2) + radius/ 1.18 * cos(angle);
92 
93  cairo_arc(w->crb,knobx1+arc_offset, knoby1+arc_offset, knob_x/2.1, 0, 2 * M_PI );
94 
96  cairo_fill (w->crb);
97  cairo_new_path (w->crb);
98 
100  cairo_arc(w->crb,knobx1+arc_offset, knoby1+arc_offset, knob_x/3.1, 0, 2 * M_PI );
101  cairo_fill_preserve(w->crb);
103  cairo_set_line_width(w->crb, knobx1/15);
104  cairo_stroke(w->crb);
105  cairo_new_path (w->crb);
106 
107  /** create a rotating pointer on the kob**/
108  cairo_set_line_cap(w->crb, CAIRO_LINE_CAP_ROUND);
109  cairo_set_line_join(w->crb, CAIRO_LINE_JOIN_BEVEL);
110  cairo_move_to(w->crb, radius_x, radius_y);
111  cairo_line_to(w->crb,lengh_x,lengh_y);
112  cairo_set_line_width(w->crb,knobx1/7);
114  cairo_stroke(w->crb);
115  cairo_new_path (w->crb);
116  }
118  cairo_text_extents_t extents;
119  /** show value on the kob**/
120  if (w->state) {
121  char s[64];
122  const char* format[] = {"%.1f", "%.2f", "%.3f"};
123  float value = adj_get_value(w->adj);
124  if (fabs(w->adj->step)>0.99) {
125  snprintf(s, 63,"%d", (int) value);
126  } else if (fabs(w->adj->step)>0.09) {
127  snprintf(s, 63, format[1-1], value);
128  } else {
129  snprintf(s, 63, format[2-1], value);
130  }
131  cairo_set_font_size (w->crb, w->app->small_font/w->scale.ascale);
132  cairo_text_extents(w->crb, s, &extents);
133  cairo_move_to (w->crb, knobx1-extents.width/2, knoby1+extents.height/2);
134  cairo_show_text(w->crb, s);
135  cairo_new_path (w->crb);
136  }
137 
138  _show_label(w, width, height);
139 }
140 
141 void _knob_released(void *w_, void* button_, void* user_data) {
142  Widget_t *w = (Widget_t*)w_;
143  if (w->flags & HAS_POINTER) w->state= 1;
144  expose_widget(w);
145 }
use_text_color_scheme
void use_text_color_scheme(Widget_t *w, Color_state st)
use_text_color_scheme - use text Colors to paint on Widget_t
Definition: xcolor.c:199
expose_widget
void expose_widget(Widget_t *w)
expose_widgets - send a expose event (EXPOSE) to a Widget_t
Definition: xwidget.c:461
Widget_t::width
int width
Definition: xwidget.h:352
Widget_t::image
cairo_surface_t * image
Definition: xwidget.h:320
adj_get_state
float adj_get_state(Adjustment_t *adj)
adj_get_state - get the current state of the Adjustment_t
Definition: xadjustment.c:148
min
#define min(x, y)
min - set a maximal value (x) as return value
Definition: xputty.h:78
get_color_state
Color_state get_color_state(Widget_t *wid)
get_color_state - get the Color_state to use in relation to the Widget_t state
Definition: xcolor.c:155
use_shadow_color_scheme
void use_shadow_color_scheme(Widget_t *w, Color_state st)
use_shadow_color_scheme - use shadow Colors to paint on Widget_t
Definition: xcolor.c:206
Xputty::normal_font
int normal_font
Definition: xputty.h:191
adj_get_value
float adj_get_value(Adjustment_t *adj)
adj_get_value - get the current value of the Adjustment_t
Definition: xadjustment.c:154
HAS_POINTER
@ HAS_POINTER
Definition: xwidget.h:247
Widget_t::crb
cairo_t * crb
Definition: xwidget.h:318
Widget_t::flags
long long flags
Definition: xwidget.h:324
Widget_t::adj
Adjustment_t * adj
Definition: xwidget.h:334
use_fg_color_scheme
void use_fg_color_scheme(Widget_t *w, Color_state st)
use_fg_color_scheme - use forground Colors to paint on Widget_t
Definition: xcolor.c:178
Widget_t::scale
Resize_t scale
Definition: xwidget.h:356
use_bg_color_scheme
void use_bg_color_scheme(Widget_t *w, Color_state st)
use_bg_color_scheme - use background Colors to paint on Widget_t
Definition: xcolor.c:185
Widget_t::app
Xputty * app
Definition: xwidget.h:300
Xputty::dpy
Display * dpy
Definition: xputty.h:181
_draw_knob_image
void _draw_knob_image(void *w_, void *user_data)
_draw_knob_image - internal draw the knob to the buffer
Definition: xknob_private.c:51
Widget_t::state
int state
Definition: xwidget.h:342
Resize_t::ascale
float ascale
Definition: xwidget.h:214
Widget_t::widget
Window widget
Definition: xwidget.h:302
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
NORMAL_
@ NORMAL_
Definition: xcolor.h:39
_knob_released
void _knob_released(void *w_, void *button_, void *user_data)
_knob_released - redraw the slider when buttob released
Definition: xknob_private.c:141
Xputty::small_font
int small_font
Definition: xputty.h:189
Widget_t::height
int height
Definition: xwidget.h:354
_draw_knob
void _draw_knob(void *w_, void *user_data)
_draw_knob - internal draw the knob to the buffer
Definition: xknob_private.c:57
Widget_t::adj_y
Adjustment_t * adj_y
Definition: xwidget.h:332
_draw_image_knob
void _draw_image_knob(Widget_t *w, int width_t, int height_t)
_draw_image_knob - internal draw the knob from image to the buffer
Definition: xknob_private.c:35
Widget_t::label
const char * label
Definition: xwidget.h:326
Adjustment_t::step
float step
Definition: xadjustment.h:92
xknob_private.h