libxputty 0.1
Loading...
Searching...
No Matches
xbutton_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 "xbutton_private.h"
23
24
25void _rounded_rectangle(cairo_t *cr,float x, float y, float width, float height) {
26 cairo_new_path (cr);
27 cairo_move_to (cr, x, (y + height)/2);
28 cairo_curve_to (cr, x ,y, x, y, (x + width)/2, y);
29 cairo_curve_to (cr, width, y, width, y, width, (y + height)/2);
30 cairo_curve_to (cr, width, height, width, height, (width + x)/2, height);
31 cairo_curve_to (cr, x, height, x, height, x, (y + height)/2);
32 cairo_close_path (cr);
33}
34
35void _pattern_out(Widget_t *w, Color_state st, int height) {
36 Colors *c = get_color_scheme(w,st);
37 if (!c) return;
38 cairo_pattern_t *pat = cairo_pattern_create_linear (2, 2, 2, height);
39 cairo_pattern_add_color_stop_rgba(pat, 0.0, c->light[0], c->light[1], c->light[2], c->light[3]);
40 cairo_pattern_add_color_stop_rgba(pat, 0.5, 0.0, 0.0, 0.0, 0.0);
41 cairo_pattern_add_color_stop_rgba(pat, 1.0, c->light[0], c->light[1], c->light[2], c->light[3]);
42 cairo_set_source(w->crb, pat);
43 cairo_pattern_destroy (pat);
44}
45
46void _pattern_in(Widget_t *w, Color_state st, int height) {
47 Colors *c = get_color_scheme(w,st);
48 if (!c) return;
49 cairo_pattern_t *pat = cairo_pattern_create_linear (2, 2, 2, height);
50 cairo_pattern_add_color_stop_rgba(pat, 0.0, 0.0, 0.0, 0.0, 0.0);
51 cairo_pattern_add_color_stop_rgba(pat, 0.5, c->light[0], c->light[1], c->light[2], c->light[3]);
52 cairo_pattern_add_color_stop_rgba(pat, 1.0, 0.0, 0.0, 0.0, 0.0);
53 cairo_set_source(w->crb, pat);
54 cairo_pattern_destroy (pat);
55}
56
57void _draw_image_button(Widget_t *w, int width_t, int height_t, float offset) {
58 int width, height;
59 os_get_surface_size(w->image, &width, &height);
60 double half_width = (width/height >=2) ? width*0.5 : width;
61 double x = (double)width_t/(double)(half_width);
62 double y = (double)height_t/(double)height;
63 double x1 = (double)height/(double)height_t;
64 double y1 = (double)(half_width)/(double)width_t;
65 double off_set = offset*x1;
66 double buttonstate = adj_get_state(w->adj);
67 int findex = (int)(((width/height)-1) * buttonstate) * (width/height >=2);
68 cairo_scale(w->crb, x,y);
69 cairo_set_source_surface (w->crb, w->image, -height*findex+off_set, off_set);
70 cairo_rectangle(w->crb,0, 0, height, height);
71 cairo_fill(w->crb);
72 cairo_scale(w->crb, x1,y1);
73}
74
75void _draw_image_button_with_label(Widget_t *w, int width_t, int height_t) {
76 int width, height;
77 os_get_surface_size(w->image, &width, &height);
78 double x = (double)width_t/(double)height;
79 double y = (double)height/(double)width_t;
80 double buttonstate = adj_get_state(w->adj);
81 int findex = (int)(((width/height)-1) * buttonstate);
82 cairo_scale(w->crb, x,x);
83 cairo_set_source_surface (w->crb, w->image, -height*findex, 0);
84 cairo_rectangle(w->crb,0, 0, height, height);
85 cairo_fill(w->crb);
86 cairo_scale(w->crb, y,y);
87 cairo_text_extents_t extents;
88 if(w->state==0) {
90 } else if(w->state==1 && ! (int)w->adj_y->value) {
92 } else if(w->state==1) {
94 } else if(w->state==2) {
96 } else if(w->state==3) {
98 }
99
101 cairo_set_font_size (w->crb,w->app->normal_font/w->scale.ascale);
102 if ((int)adj_get_value(w->adj) && strlen(w->input_label)) {
103 cairo_text_extents(w->crb,w->input_label , &extents);
104 cairo_move_to (w->crb, (width_t*0.5)-(extents.width/2), height_t-(extents.height/4));
105 cairo_show_text(w->crb, w->input_label);
106 } else {
107 cairo_text_extents(w->crb,w->label , &extents);
108 cairo_move_to (w->crb, (width_t*0.5)-(extents.width/2), height_t-(extents.height/4));
109 cairo_show_text(w->crb, w->label);
110 }
111 cairo_new_path (w->crb);
112}
113
114void _draw_switch_image_button(void *w_, void* user_data) {
115 Widget_t *w = (Widget_t*)w_;
116 if (!w) return;
117 Metrics_t metrics;
118 os_get_window_metrics(w, &metrics);
119 int width = metrics.width-2;
120 int height = metrics.height-2;
121 if (!metrics.visible) return;
122 if(strlen(w->label)) {
123 _draw_image_button_with_label(w, width, height);
124 } else {
125 _draw_image_button(w, width, height,0.0);
126 }
127}
128
129void _draw_button_base(Widget_t *w, int width, int height) {
130 if (!w->state && (int)w->adj_y->value) {
131 w->state = 3;
132 } else if (w->state == 3 && !(int)w->adj_y->value) {
133 w->state = 0;
134 }
135
136 _rounded_rectangle(w->crb,2.0, 2.0, width, height);
137
138 if(w->state==0) {
139 cairo_set_line_width(w->crb, 1.0);
140 _pattern_out(w, NORMAL_, height);
141 cairo_fill_preserve(w->crb);
143 } else if(w->state==1) {
144 _pattern_out(w, PRELIGHT_, height);
145 cairo_fill_preserve(w->crb);
146 cairo_set_line_width(w->crb, 1.5);
148 } else if(w->state==2) {
149 _pattern_in(w, SELECTED_, height);
150 cairo_fill_preserve(w->crb);
151 cairo_set_line_width(w->crb, 1.0);
153 } else if(w->state==3) {
154 _pattern_in(w, ACTIVE_, height);
155 cairo_fill_preserve(w->crb);
156 cairo_set_line_width(w->crb, 1.0);
158 }
159 cairo_stroke(w->crb);
160
161 if(w->state==2) {
162 _rounded_rectangle(w->crb,4.0, 4.0, width, height);
163 cairo_stroke(w->crb);
164 _rounded_rectangle(w->crb,3.0, 3.0, width, height);
165 cairo_stroke(w->crb);
166 } else if (w->state==3) {
167 _rounded_rectangle(w->crb,3.0, 3.0, width, height);
168 cairo_stroke(w->crb);
169 }
170}
171
172int _remove_low_dash(char *str) {
173
174 char *src;
175 char *dst;
176 int i = 0;
177 int r = 0;
178 for (src = dst = str; *src != '\0'; src++) {
179 *dst = *src;
180 if (*dst != '_') {
181 dst++;
182 } else {
183 r = i;
184 }
185 i++;
186 }
187 *dst = '\0';
188 return r;
189}
190
191void _draw_button(void *w_, void* user_data) {
192 Widget_t *w = (Widget_t*)w_;
193 if (!w) return;
194 Metrics_t metrics;
195 os_get_window_metrics(w, &metrics);
196 int width = metrics.width-2;
197 int height = metrics.height-2;
198 if (!metrics.visible) return;
199
200 float offset = 0.0;
201 if(w->state==1 && ! (int)w->adj_y->value) {
202 offset = 1.0;
203 } else if(w->state==1) {
204 offset = 2.0;
205 } else if(w->state==2) {
206 offset = 2.0;
207 } else if(w->state==3) {
208 offset = 1.0;
209 }
210
211 if (w->image) {
212 if(strlen(w->label)) {
213 _draw_image_button_with_label(w, width, height);
214 } else {
215 _draw_image_button(w, width, height,offset);
216 }
217 }
218}
219
220void _draw_base_button(void *w_, void* user_data) {
221 Widget_t *w = (Widget_t*)w_;
222 if (!w) return;
223 Metrics_t metrics;
224 os_get_window_metrics(w, &metrics);
225 int width = metrics.width-2;
226 int height = metrics.height-2;
227 if (!metrics.visible) return;
228 _draw_button_base(w, width, height);
229
230 float offset = 0.0;
231 if(w->state==1 && ! (int)w->adj_y->value) {
232 offset = 1.0;
233 } else if(w->state==1) {
234 offset = 2.0;
235 } else if(w->state==2) {
236 offset = 2.0;
237 } else if(w->state==3) {
238 offset = 1.0;
239 }
240 if (w->image) {
241 if(strlen(w->label)) {
242 _draw_image_button_with_label(w, width, height);
243 } else {
244 _draw_image_button(w, width, height,offset);
245 }
246 } else {
247
248 cairo_text_extents_t extents;
250 cairo_set_font_size (w->crb, w->app->normal_font/w->scale.ascale);
251
252 if (strstr(w->label, "_")) {
253 cairo_text_extents(w->crb, "--", &extents);
254 double underline = extents.width;
255 strncpy(w->input_label,w->label, sizeof(w->input_label)-1);
256 int pos = _remove_low_dash(w->input_label);
257 int len = strlen(w->input_label);
258 cairo_text_extents(w->crb,w->input_label , &extents);
259 int set_line = (extents.width/len) * pos;
260 cairo_move_to (w->crb, (width-extents.width)*0.5 +offset, (height+extents.height)*0.5 +offset);
261 cairo_show_text(w->crb, w->input_label);
262 cairo_set_line_width(w->crb, 1.0);
263 cairo_move_to (w->crb, (width-extents.width)*0.5 +offset + set_line, (height+extents.height)*0.55 +offset);
264 cairo_line_to(w->crb,(width-extents.width)*0.5 +offset + set_line + underline, (height+extents.height)*0.55 +offset);
265 cairo_stroke(w->crb);
266 } else {
267 cairo_text_extents(w->crb,w->label , &extents);
268 cairo_move_to (w->crb, (width-extents.width)*0.5 +offset, (height+extents.height)*0.5 +offset);
269 cairo_show_text(w->crb, w->label);
270 }
271 }
272}
273
274void _draw_on_off_button(void *w_, void* user_data) {
275 Widget_t *w = (Widget_t*)w_;
276 if (!w) return;
277 Metrics_t metrics;
278 os_get_window_metrics(w, &metrics);
279 int width = metrics.width-2;
280 int height = metrics.height-2;
281 if (!metrics.visible) return;
282
283 _draw_button_base(w, width, height);
284
285 float offset = 0.0;
286 cairo_text_extents_t extents;
287 if(w->state==1 && ! (int)w->adj_y->value) {
288 offset = 1.0;
289 } else if(w->state==1) {
290 offset = 2.0;
291 } else if(w->state==2) {
292 offset = 2.0;
293 } else if(w->state==3) {
294 offset = 1.0;
295 }
296 if((int)w->adj_y->value) {
297 w->label = "On";
298 } else {
299 w->label = "Off";
300 }
301
303 cairo_set_font_size (w->crb, w->app->normal_font/w->scale.ascale);
304 cairo_text_extents(w->crb,w->label , &extents);
305 if(IS_UTF8(w->label[0])) {
306 cairo_set_font_size (w->crb, w->app->normal_font/w->scale.ascale);
307 cairo_text_extents(w->crb,w->label , &extents);
308 }
309
310 cairo_move_to (w->crb, (width-extents.width)*0.5 +offset, (height+extents.height)*0.5 +offset);
311 cairo_show_text(w->crb, w->label);
312 cairo_new_path (w->crb);
313
314}
315
316void _draw_ti_button(void *w_, void* user_data) {
317 Widget_t *w = (Widget_t*)w_;
318 if (!w) return;
319 Metrics_t metrics;
320 os_get_window_metrics(w, &metrics);
321 int width = metrics.width-2;
322 int height = metrics.height-2;
323 if (!metrics.visible) return;
324 _draw_button_base(w, width, height);
325 if (w->image) {
326 float offset = 0.0;
327 if(w->state==1 && ! (int)w->adj_y->value) {
328 offset = 1.0;
329 } else if(w->state==1) {
330 offset = 2.0;
331 } else if(w->state==2) {
332 offset = 2.0;
333 } else if(w->state==3) {
334 offset = 1.0;
335 }
336
337 _draw_image_button(w, width, height,offset);
338 }
339}
340
341void _draw_check_button(void *w_, void* user_data) {
342 Widget_t *w = (Widget_t*)w_;
343 if (!w) return;
344 Metrics_t metrics;
345 os_get_window_metrics(w, &metrics);
346 int width = metrics.width-2;
347 int height = metrics.height-2;
348 if (!metrics.visible) return;
349 if (w->image) {
350 _draw_image_button(w, width, height,0.0);
351 } else {
352 _draw_button_base(w, width, height);
353
354 if(w->state==3) {
356 float offset = 1.0;
357 int wa = width/1.3;
358 int h = height/2.2;
359 int wa1 = width/2.2;
360 int h1 = height/1.3;
361 int wa2 = width/2.8;
362
363 cairo_set_line_width(w->crb, 2.5);
364 cairo_move_to(w->crb, wa+offset, h+offset);
365 cairo_line_to(w->crb, wa1+offset, h1+offset);
366 cairo_line_to(w->crb, wa2+offset, h+offset);
367 cairo_stroke(w->crb);
368 }
369
370 cairo_new_path (w->crb);
371 }
372}
373
374void _draw_check_box(void *w_, void* user_data) {
375 Widget_t *w = (Widget_t*)w_;
376 if (!w) return;
377 Metrics_t metrics;
378 os_get_window_metrics(w, &metrics);
379 int height = metrics.height-2;
380 if (!metrics.visible) return;
381 if (w->image) {
382 _draw_image_button(w, height, height,0.0);
383 } else {
384 _draw_button_base(w, height, height);
385
386 if(adj_get_value(w->adj)) {
388 float offset = 1.0;
389 int wa = height/1.3;
390 int h = height/2.2;
391 int wa1 = height/2.2;
392 int h1 = height/1.3;
393 int wa2 = height/2.8;
394
395 cairo_set_line_width(w->crb, 2.5);
396 cairo_move_to(w->crb, wa+offset, h+offset);
397 cairo_line_to(w->crb, wa1+offset, h1+offset);
398 cairo_line_to(w->crb, wa2+offset, h+offset);
399 cairo_stroke(w->crb);
400 }
401
402 cairo_new_path (w->crb);
403
404 cairo_text_extents_t extents;
406 cairo_set_font_size (w->crb, w->app->normal_font/w->scale.ascale);
407 cairo_text_extents(w->crb,w->label , &extents);
408 cairo_move_to (w->crb, height+5 , (height+extents.height)*0.5 );
409 cairo_show_text(w->crb, w->label);
410 cairo_new_path (w->crb);
411 }
412}
413
414/*---------------------------------------------------------------------
415-----------------------------------------------------------------------
416 button
417-----------------------------------------------------------------------
418----------------------------------------------------------------------*/
419
420void _button_pressed(void *w_, void* button, void* user_data) {
421 Widget_t *w = (Widget_t*)w_;
422 adj_set_value(w->adj_y, 1.0);
423}
424
425void _button_released(void *w_, void* button_, void* user_data) {
426 Widget_t *w = (Widget_t*)w_;
427 if (w->flags & HAS_POINTER) w->state=1;
428 adj_set_value(w->adj_y, 0.0);
429}
430
431/*---------------------------------------------------------------------
432-----------------------------------------------------------------------
433 toggle button
434-----------------------------------------------------------------------
435----------------------------------------------------------------------*/
436
437void _toggle_button_pressed(void *w_, void* button, void* user_data) {
438 Widget_t *w = (Widget_t*)w_;
439 expose_widget(w);
440}
441
442void _toggle_button_released(void *w_, void* button_, void* user_data) {
443 Widget_t *w = (Widget_t*)w_;
444 XButtonEvent *xbutton = (XButtonEvent*)button_;
445 if (w->flags & HAS_POINTER) {
446 float value = w->adj->value;
447 if (xbutton->button == Button1) value = value ?
448 w->adj->min_value : w->adj->max_value;
449 if (xbutton->button == Button4) value = w->adj->max_value;
450 if (xbutton->button == Button5) value = w->adj->min_value;
451 adj_set_value(w->adj, value);
452 w->state = (int) w->adj->value ? 3 : 1;
453 } else {
454 w->state = (int) w->adj->value ? 3 : 0;
455 }
456 expose_widget(w);
457}
float min_value
Definition xadjustment.h:90
float max_value
Definition xadjustment.h:92
Color_t - struct used to set cairo color for Widget_t.
Definition xcolor.h:85
double light[4]
Definition xcolor.h:92
Metrics_t - struct to receive window size, position & visibility Pass this struct to os_get_window_...
float ascale
Definition xwidget.h:369
Widget_t - struct to hold the basic Widget_t info.
Definition xwidget.h:457
Resize_t scale
Definition xwidget.h:525
Adjustment_t * adj_y
Definition xwidget.h:495
cairo_surface_t * image
Definition xwidget.h:491
Adjustment_t * adj
Definition xwidget.h:497
cairo_t * crb
Definition xwidget.h:489
int state
Definition xwidget.h:511
long long flags
Definition xwidget.h:461
char input_label[32]
Definition xwidget.h:459
const char * label
Definition xwidget.h:463
Xputty * app
Definition xwidget.h:465
int button
int normal_font
Definition xputty.h:248
void adj_set_value(Adjustment_t *adj, float value)
adj_set_value - set the current value to the Adjustment_t
float adj_get_value(Adjustment_t *adj)
adj_get_value - get the current value of the Adjustment_t
float adj_get_state(Adjustment_t *adj)
adj_get_state - get the current state of the Adjustment_t
void _button_released(void *w_, void *button_, void *user_data)
_button_released - redraw the button and send state via user_callback
void _draw_switch_image_button(void *w_, void *user_data)
draw switch_image_button - internal draw the button to the buffer
int _remove_low_dash(char *str)
void _toggle_button_released(void *w_, void *button_, void *user_data)
_toggle_button_released - redraw the button and send state via user_callback
void _pattern_out(Widget_t *w, Color_state st, int height)
_pattern_in - a little pattern to make press state more visible
void _draw_button_base(Widget_t *w, int width, int height)
void _button_pressed(void *w_, void *button, void *user_data)
_button_pressed - redraw the button and send state via user_callback
void _toggle_button_pressed(void *w_, void *button, void *user_data)
_toggle_button_pressed - redraw the button and send state via user_callback
void _draw_base_button(void *w_, void *user_data)
_draw_base_button - internal draw the button with base to the buffer
void _draw_check_button(void *w_, void *user_data)
_draw_check_button - internal draw the button to the buffer
void _draw_button(void *w_, void *user_data)
_draw_button - internal draw the button to the buffer
void _draw_image_button(Widget_t *w, int width_t, int height_t, float offset)
void _pattern_in(Widget_t *w, Color_state st, int height)
_pattern_in - a little pattern to make press state more visible
void _draw_on_off_button(void *w_, void *user_data)
_draw_on_off_button - internal draw the on/off button to the buffer
void _draw_check_box(void *w_, void *user_data)
_draw_check_box - internal draw the check box to the buffer
void _draw_ti_button(void *w_, void *user_data)
_draw_ti_button - internal draw the button to the buffer
void _rounded_rectangle(cairo_t *cr, float x, float y, float width, float height)
_rounded_rectangle - internal draw a rounded button
void _draw_image_button_with_label(Widget_t *w, int width_t, int height_t)
void use_frame_color_scheme(Widget_t *w, Color_state st)
use_frame_color_scheme - use frame Colors to paint on Widget_t
Definition xcolor.c:280
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:222
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:266
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:245
Colors * get_color_scheme(Widget_t *wid, Color_state st)
get_color_scheme - get pointer to the Colors struct to use in relation to the Color_state
Definition xcolor.c:197
Color_state
Color_state - select color mode to use on Widget_t.
Definition xcolor.h:43
@ NORMAL_
Definition xcolor.h:44
@ SELECTED_
Definition xcolor.h:46
@ PRELIGHT_
Definition xcolor.h:45
@ ACTIVE_
Definition xcolor.h:47
void os_get_window_metrics(Widget_t *w, Metrics_t *metrics)
os_get_window_metrics - Get the Merics_t struct related to a Widget_t
void os_get_surface_size(cairo_surface_t *surface, int *width, int *height)
os_get_surface_size - get the size of the cairo surface
void expose_widget(Widget_t *w)
expose_widgets - send a expose event (EXPOSE) to a Widget_t
Definition xwidget.c:445
@ HAS_POINTER
Definition xwidget.h:402