libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xknob_private.c File Reference
#include "xknob_private.h"
Include dependency graph for xknob_private.c:

Go to the source code of this file.

Functions

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 More...
 
void _draw_knob_image (void *w_, void *user_data)
 _draw_knob_image - internal draw the knob to the buffer More...
 
void _draw_knob (void *w_, void *user_data)
 _draw_knob - internal draw the knob to the buffer More...
 
void _knob_released (void *w_, void *button_, void *user_data)
 _knob_released - redraw the slider when buttob released More...
 

Function Documentation

◆ _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

Parameters
*w- pointer to the Widget_t knob
Returns
void

Definition at line 35 of file xknob_private.c.

35  {
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 }

References adj_get_state(), Widget_t::adj_y, Widget_t::crb, and Widget_t::image.

◆ _draw_knob()

void _draw_knob ( void *  w_,
void *  user_data 
)

_draw_knob - internal draw the knob to the buffer

Parameters
*w_- void pointer to the Widget_t button
*user_data- void pointer to attached user_data
Returns
void

get values for the knob

create a rotating pointer on the kob

show value on the kob

Definition at line 57 of file xknob_private.c.

57  {
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 }

References _draw_image_knob(), Widget_t::adj, adj_get_state(), adj_get_value(), Widget_t::adj_y, Widget_t::app, Resize_t::ascale, Widget_t::crb, Xputty::dpy, get_color_state(), Widget_t::image, min, NORMAL_, Widget_t::scale, Xputty::small_font, Widget_t::state, Adjustment_t::step, use_bg_color_scheme(), use_fg_color_scheme(), use_shadow_color_scheme(), use_text_color_scheme(), and Widget_t::widget.

◆ _draw_knob_image()

void _draw_knob_image ( void *  w_,
void *  user_data 
)

_draw_knob_image - internal draw the knob to the buffer

Parameters
*w_- void pointer to the Widget_t button
*user_data- void pointer to attached user_data
Returns
void

Definition at line 51 of file xknob_private.c.

51  {
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 }

References _draw_image_knob(), Widget_t::height, and Widget_t::width.

◆ _knob_released()

void _knob_released ( void *  w_,
void *  button_,
void *  user_data 
)

_knob_released - redraw the slider when buttob released

Parameters
*w_- void pointer to the Widget_t button
*user_data- void pointer to attached user_data
Returns
void

Definition at line 141 of file xknob_private.c.

141  {
142  Widget_t *w = (Widget_t*)w_;
143  if (w->flags & HAS_POINTER) w->state= 1;
144  expose_widget(w);
145 }

References expose_widget(), Widget_t::flags, HAS_POINTER, and Widget_t::state.

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
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
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
Xputty::small_font
int small_font
Definition: xputty.h:189
Widget_t::height
int height
Definition: xwidget.h:354
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
Adjustment_t::step
float step
Definition: xadjustment.h:92