libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xlistbox_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 "xlistbox_private.h"
23 #include "xtooltip.h"
24 
25 
26 void _draw_listbox(void *w_, void* user_data) {
27  Widget_t *w = (Widget_t*)w_;
28  if (!w) return;
30  cairo_paint (w->crb);
31 }
32 
33 void _draw_listbox_item(void *w_, void* user_data) {
34  Widget_t *w = (Widget_t*)w_;
35  if (!w) return;
36  XWindowAttributes attrs;
37  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
38  int width = attrs.width;
39  int height = attrs.height;
40  if (attrs.map_state != IsViewable) return;
41  Widget_t* view_port = (Widget_t*)w->parent;
42  Widget_t* listbox = (Widget_t*)view_port->parent;
43  int j = (int)listbox->adj->value;
44  if(w == view_port->childlist->childs[j])
45  w->state = (w->state == 1) ? 1 : 3;
46 
48  cairo_rectangle(w->crb, 0, 0, width , height);
49  if(w->state==1) {
51  } else if(w->state==2) {
53  } else if(w->state==3) {
55  }
56  cairo_fill_preserve(w->crb);
57  cairo_set_line_width(w->crb, 1.0);
59  cairo_stroke(w->crb);
60  cairo_text_extents_t extents;
61  /** show label **/
63  cairo_set_font_size (w->crb, height/2);
64  cairo_text_extents(w->crb,w->label , &extents);
65 
66  cairo_move_to (w->crb, (width-extents.width)/2., height - extents.height );
67  cairo_show_text(w->crb, w->label);
68  cairo_new_path (w->crb);
69  if (extents.width > (float)width) {
70  tooltip_set_text(w,w->label);
71  w->flags |= HAS_TOOLTIP;
72  } else {
73  w->flags &= ~HAS_TOOLTIP;
74  }
75 }
76 
77 void _reconfigure_listbox_viewport(void *w_, void* user_data) {
78  Widget_t *w = (Widget_t*)w_;
79  float st = adj_get_state(w->adj);
80  Widget_t* listbox = (Widget_t*)w->parent;
81  XWindowAttributes attrs;
82  XGetWindowAttributes(listbox->app->dpy, (Window)listbox->widget, &attrs);
83  int height = attrs.height;
84  int elem = height/25;
85  int si = childlist_has_child(w->childlist);
86  w->adj->max_value = si-elem;
87  adj_set_state(w->adj,st);
88 }
89 
90 void _configure_listbox(void *w_, void* user_data) {
91  Widget_t *w = (Widget_t*)w_;
92  int si = max(1,childlist_has_child(w->childlist));
93  Widget_t* listbox = (Widget_t*)w->parent;
94  XWindowAttributes attrs;
95  XGetWindowAttributes(listbox->app->dpy, (Window)listbox->widget, &attrs);
96  int width = attrs.width;
97  XResizeWindow (w->app->dpy, w->widget, width, 25*(si));
98 }
99 
100 void _draw_listbox_viewslider(void *w_, void* user_data) {
101  Widget_t *w = (Widget_t*)w_;
102  int v = (int)w->adj->max_value;
103  if (!v) return;
104  XWindowAttributes attrs;
105  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
106  if (attrs.map_state != IsViewable) return;
107  int width = attrs.width;
108  int height = attrs.height;
109  float sliderstate = adj_get_state(w->adj);
111  cairo_rectangle(w->crb, width-5,0,5,height);
112  cairo_fill_preserve(w->crb);
114  cairo_fill(w->crb);
116  cairo_rectangle(w->crb, width-5,(height-10)*sliderstate,5,10);
117  cairo_fill_preserve(w->crb);
119  cairo_set_line_width(w->crb,1);
120  cairo_stroke(w->crb);
121 }
122 
123 void _set_listbox_viewpoint(void *w_, void* user_data) {
124  Widget_t *w = (Widget_t*)w_;
125  int v = (int)adj_get_value(w->adj);
126  XWindowAttributes attrs;
127  XGetWindowAttributes(w->app->dpy, (Window)w->childlist->childs[0]->widget, &attrs);
128  int height = attrs.height;
129  XMoveWindow(w->app->dpy,w->widget,0, -height*v);
130 }
131 
132 void _listbox_entry_released(void *w_, void* button_, void* user_data) {
133  Widget_t *w = (Widget_t*)w_;
134  Widget_t* view_port = (Widget_t*)w->parent;
135  int direction = 0 ;
136  if (w->flags & HAS_POINTER) {
137  XButtonEvent *xbutton = (XButtonEvent*)button_;
138  if(xbutton->button == Button1) {
139  Widget_t* listbox = (Widget_t*)view_port->parent;
140  int i = view_port->childlist->elem-1;
141  int old_value = (int) listbox->adj->value;
142  for(;i>-1;i--) {
143  Widget_t *wid = view_port->childlist->childs[i];
144  if (xbutton->window == wid->widget) {
145  const char *l = view_port->childlist->childs[i]->label;
146  float value = (float)i;
147  check_value_changed(listbox->adj, &value);
148  wid->state= 3;
149  listbox->func.button_release_callback(listbox, &i, &l);
150  }
151  wid->state= 0;
152  }
153  expose_widget(view_port->childlist->childs[old_value]);
154  expose_widget(w);
155  } else if(xbutton->button == Button4) {
156  direction = 1;
157  } else if(xbutton->button == Button5) {
158  direction = -1;
159  }
160  if (direction !=0) {
161  float value = view_port->adj->value + (view_port->adj->step * -direction);
162  if (value>view_port->adj->max_value) value = view_port->adj->max_value;
163  if (value<view_port->adj->min_value) value = view_port->adj->min_value;
164  check_value_changed(view_port->adj, &value);
165  }
166  }
167 }
168 
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
HAS_TOOLTIP
@ HAS_TOOLTIP
Definition: xwidget.h:249
expose_widget
void expose_widget(Widget_t *w)
expose_widgets - send a expose event (EXPOSE) to a Widget_t
Definition: xwidget.c:461
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
Adjustment_t::value
float value
Definition: xadjustment.h:86
_draw_listbox_item
void _draw_listbox_item(void *w_, void *user_data)
_draw_listbox_item - draw item on expose call
Definition: xlistbox_private.c:33
Childlist_t::childs
Widget_t ** childs
Definition: xchildlist.h:51
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
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
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
use_frame_color_scheme
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:213
Widget_t::state
int state
Definition: xwidget.h:342
ACTIVE_
@ ACTIVE_
Definition: xcolor.h:42
Widget_t::widget
Window widget
Definition: xwidget.h:302
_set_listbox_viewpoint
void _set_listbox_viewpoint(void *w_, void *user_data)
_set_listbox_viewpoint - move the view_port to position
Definition: xlistbox_private.c:123
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
NORMAL_
@ NORMAL_
Definition: xcolor.h:39
PRELIGHT_
@ PRELIGHT_
Definition: xcolor.h:40
Func_t::button_release_callback
evfunc button_release_callback
Definition: xwidget.h:94
Widget_t::childlist
Childlist_t * childlist
Definition: xwidget.h:336
Adjustment_t::max_value
float max_value
Definition: xadjustment.h:90
_draw_listbox
void _draw_listbox(void *w_, void *user_data)
_draw_listbox - draw listbox on expose call
Definition: xlistbox_private.c:26
adj_set_state
void adj_set_state(Adjustment_t *adj, float state)
adj_set_state - set the current state of the Adjustment_t
Definition: xadjustment.c:141
Childlist_t::elem
int elem
Definition: xchildlist.h:57
_draw_listbox_viewslider
void _draw_listbox_viewslider(void *w_, void *user_data)
_draw_listbox_viewslider - draw a slider on the viewport to indicate the view point
Definition: xlistbox_private.c:100
Widget_t::func
Func_t func
Definition: xwidget.h:310
_reconfigure_listbox_viewport
void _reconfigure_listbox_viewport(void *w_, void *user_data)
_reconfigure_listbox_viewport - reconfigure the viewport adjustment on size changes
Definition: xlistbox_private.c:77
childlist_has_child
int childlist_has_child(Childlist_t *childlist)
childlist_has_child - check if a Widget_t Childlist_t contain a child
Definition: xchildlist.c:91
max
#define max(x, y)
max - set a minimal value (x) as return value
Definition: xputty.h:86
Widget_t::parent
void * parent
Definition: xwidget.h:304
tooltip_set_text
void tooltip_set_text(Widget_t *w, const char *label)
tooltip_set_text - set a (new) text to a tooltip for Widget_t
Definition: xtooltip.c:25
_listbox_entry_released
void _listbox_entry_released(void *w_, void *button_, void *user_data)
_listbox_entry_released - redraw the slider when buttob released
Definition: xlistbox_private.c:132
use_base_color_scheme
void use_base_color_scheme(Widget_t *w, Color_state st)
use_base_color_scheme - use base Colors to paint on Widget_t
Definition: xcolor.c:192
check_value_changed
void check_value_changed(Adjustment_t *adj, float *value)
check_value_changed - check if Adjustment_t value have changed and send value_changed_callback (VALUE...
Definition: xadjustment.c:241
xtooltip.h
SELECTED_
@ SELECTED_
Definition: xcolor.h:41
Widget_t::label
const char * label
Definition: xwidget.h:326
Adjustment_t::step
float step
Definition: xadjustment.h:92
xlistbox_private.h
Adjustment_t::min_value
float min_value
Definition: xadjustment.h:88
_configure_listbox
void _configure_listbox(void *w_, void *user_data)
_configure_listbox - configure the viewport on mapping
Definition: xlistbox_private.c:90