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 "xvaluedisplay_private.h"
23
24
_draw_valuedisplay(void * w_,void * user_data)25 void _draw_valuedisplay(void *w_, void* user_data) {
26 Widget_t *w = (Widget_t*)w_;
27 if (!w) return;
28 XWindowAttributes attrs;
29 XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
30 int width = attrs.width-2;
31 int height = attrs.height-2;
32 if (attrs.map_state != IsViewable) return;
33
34 cairo_rectangle(w->crb,2.0, 2.0, width, height);
35
36 if(w->state==0) {
37 cairo_set_line_width(w->crb, 1.0);
38 use_shadow_color_scheme(w, NORMAL_);
39 cairo_fill_preserve(w->crb);
40 use_frame_color_scheme(w, NORMAL_);
41 } else if(w->state==1) {
42 use_shadow_color_scheme(w, PRELIGHT_);
43 cairo_fill_preserve(w->crb);
44 cairo_set_line_width(w->crb, 1.5);
45 use_frame_color_scheme(w, NORMAL_);
46 } else if(w->state==2) {
47 use_shadow_color_scheme(w, SELECTED_);
48 cairo_fill_preserve(w->crb);
49 cairo_set_line_width(w->crb, 1.0);
50 use_frame_color_scheme(w, SELECTED_);
51 } else if(w->state==3) {
52 use_shadow_color_scheme(w, ACTIVE_);
53 cairo_fill_preserve(w->crb);
54 cairo_set_line_width(w->crb, 1.0);
55 use_frame_color_scheme(w, ACTIVE_);
56 } else if(w->state==4) {
57 use_shadow_color_scheme(w, INSENSITIVE_);
58 cairo_fill_preserve(w->crb);
59 cairo_set_line_width(w->crb, 1.0);
60 use_frame_color_scheme(w, INSENSITIVE_);
61 }
62 cairo_stroke(w->crb);
63
64 cairo_rectangle(w->crb,4.0, 4.0, width, height);
65 cairo_stroke(w->crb);
66 cairo_rectangle(w->crb,3.0, 3.0, width, height);
67 cairo_stroke(w->crb);
68
69 cairo_text_extents_t extents;
70
71 char s[64];
72 const char* format[] = {"%.1f ", "%.2f ", "%. Hz"};
73 float value = adj_get_value(w->adj);
74 snprintf(s, 63, format[2-1], value);
75
76
77 use_text_color_scheme(w, get_color_state(w));
78 float font_size = w->app->normal_font/w->scale.ascale;
79 cairo_set_font_size (w->crb, font_size);
80 cairo_text_extents(w->crb,s , &extents);
81 cairo_move_to (w->crb, (width-extents.width)*0.5, (height+extents.height)*0.55);
82 cairo_show_text(w->crb, s);
83 cairo_new_path (w->crb);
84
85 }
86
_draw_spinbox(void * w_,void * user_data)87 void _draw_spinbox(void *w_, void* user_data) {
88 Widget_t *w = (Widget_t*)w_;
89 Widget_t *p = (Widget_t*)w->parent;
90 Widget_t *parent = (Widget_t*)p->parent;
91 if (!w) return;
92 XWindowAttributes attrs;
93 XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
94 int width = attrs.width-2;
95 int height = attrs.height-2;
96 if (attrs.map_state != IsViewable) return;
97
98 cairo_rectangle(w->crb,2.0, 2.0, width, height);
99 use_bg_color_scheme(w, NORMAL_);
100 cairo_fill_preserve(w->crb);
101 use_text_color_scheme(w, NORMAL_);
102 cairo_stroke(w->crb);
103
104 cairo_text_extents_t extents;
105
106 char s[64];
107 const char* format[] = {"%.1f ", "%.2f ", "%. Hz"};
108 float value = adj_get_value(parent->adj);
109 snprintf(s, 63, format[2-1], value);
110
111
112 use_text_color_scheme(w, get_color_state(w));
113 float font_size = w->app->normal_font/w->scale.ascale;
114 cairo_set_font_size (w->crb, font_size);
115 cairo_text_extents(w->crb,s , &extents);
116 cairo_move_to (w->crb, (width-extents.width)*0.5, (height+extents.height)*0.55);
117 cairo_show_text(w->crb, s);
118 cairo_new_path (w->crb);
119 }
120
_draw_buttons(void * w_,void * user_data)121 void _draw_buttons(void *w_, void* user_data) {
122 Widget_t *w = (Widget_t*)w_;
123 XWindowAttributes attrs;
124 XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
125 int width = attrs.width;
126 int height = attrs.height;
127 if (attrs.map_state != IsViewable) return;
128
129 cairo_rectangle(w->crb,0.0, 2.0, width, height-2);
130 use_bg_color_scheme(w, NORMAL_);
131 cairo_fill_preserve(w->crb);
132 use_text_color_scheme(w, NORMAL_);
133 cairo_stroke(w->crb);
134 cairo_rectangle(w->crb,0.0, height/2, width, height-2);
135 cairo_stroke(w->crb);
136 use_text_color_scheme(w, get_color_state(w));
137 float font_size = w->app->big_font/w->scale.ascale;
138 cairo_set_font_size (w->crb, font_size);
139 cairo_move_to (w->crb, 5,18);
140 cairo_show_text(w->crb, "+");
141 cairo_move_to (w->crb, 7,38);
142 cairo_show_text(w->crb, "-");
143 }
144
_buttons_released(void * w_,void * button_,void * user_data)145 void _buttons_released(void *w_, void* button_, void* user_data) {
146 Widget_t *w = (Widget_t*)w_;
147 Widget_t *p = (Widget_t*)w->parent;
148 Widget_t *parent = (Widget_t*)p->parent;
149 XButtonEvent *xbutton = (XButtonEvent*)button_;
150 if (!w) return;
151 XWindowAttributes attrs;
152 XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
153 int height = attrs.height;
154 if (attrs.map_state != IsViewable) return;
155 if (xbutton->button == Button1) {
156 if (xbutton->y > height/2)
157 adj_set_value(parent->adj, adj_get_value(parent->adj)-parent->adj->step);
158 else
159 adj_set_value(parent->adj, adj_get_value(parent->adj)+parent->adj->step);
160 expose_widget(p);
161 } else if (xbutton->button == Button4) {
162 adj_set_value(parent->adj, adj_get_value(parent->adj)+parent->adj->step);
163 expose_widget(p);
164 } else if (xbutton->button == Button5) {
165 adj_set_value(parent->adj, adj_get_value(parent->adj)-parent->adj->step);
166 expose_widget(p);
167 }
168 }
169
_popup_spinbox(void * w_,void * button,void * user_data)170 void _popup_spinbox(void *w_, void* button, void* user_data) {
171 Widget_t *w = (Widget_t*)w_;
172 Widget_t *spin_box = (Widget_t*)w->childlist->childs[0];
173 int x1, y1;
174 Window child;
175 XTranslateCoordinates( w->app->dpy, w->widget, DefaultRootWindow(w->app->dpy), 0, 0, &x1, &y1, &child );
176 XMoveWindow(spin_box->app->dpy,spin_box->widget,x1-10, y1-10);
177 pop_widget_show_all(spin_box);
178 int err = XGrabPointer(w->app->dpy, DefaultRootWindow(w->app->dpy), True,
179 ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
180 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
181 w->app->hold_grab = spin_box;
182 if (err) debug_print("Error grap pointer\n");
183 }
184