libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xlistview_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 "xlistview_private.h"
23 #include "xtooltip.h"
24 
25 
26 void _draw_listview(void *w_, void* user_data) {
27  Widget_t *w = (Widget_t*)w_;
28  if (!w) return;
30  cairo_paint (w->cr);
31 }
32 
33 void _draw_list(void *w_, void* user_data) {
34  Widget_t *w = (Widget_t*)w_;
35  _draw_listview_viewslider(w, user_data);
36  XWindowAttributes attrs;
37  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
38  if (attrs.map_state != IsViewable) return;
39  int width = attrs.width;
40  int height = attrs.height;
41  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
42 
43  int v = (int)w->adj->max_value;
44  int sub = (v<=0) ? 0 : 5;
46  cairo_rectangle(w->crb, 0, 0, width-sub , height);
47  cairo_fill (w->crb);
48 
49  int i = (int)max(0,adj_get_value(w->adj));
50  int j = filelist->list_size<filelist->show_items+i+1 ?
51  filelist->list_size : filelist->show_items+i+1;
52  for(;i<j;i++) {
53  if(i == filelist->prelight_item && i == filelist->active_item)
55  else if(i == filelist->prelight_item)
57  else if (i == filelist->active_item)
59  else
61  cairo_rectangle(w->crb, 0, i*25, width-sub , 25);
62  cairo_fill_preserve(w->crb);
63  cairo_set_line_width(w->crb, 1.0);
65  cairo_stroke(w->crb);
66  cairo_text_extents_t extents;
67  /** show label **/
68  if(i == filelist->prelight_item && i == filelist->active_item)
70  else if(i == filelist->prelight_item)
72  else if (i == filelist->active_item)
74  else
76  cairo_set_font_size (w->crb, 12);
77  cairo_text_extents(w->crb,filelist->list_names[i] , &extents);
78 
79  cairo_move_to (w->crb, (width-extents.width)/2., (25*(i+1)) - extents.height );
80  cairo_show_text(w->crb, filelist->list_names[i]);
81  cairo_new_path (w->crb);
82  if (i == filelist->prelight_item && extents.width > (float)width-20) {
83  tooltip_set_text(w,filelist->list_names[i]);
84  w->flags |= HAS_TOOLTIP;
85  show_tooltip(w);
86  } else {
87  w->flags &= ~HAS_TOOLTIP;
88  hide_tooltip(w);
89  }
90  }
91 }
92 
93 void _list_motion(void *w_, void* xmotion_, void* user_data) {
94  Widget_t *w = (Widget_t*)w_;
95  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
96  XMotionEvent *xmotion = (XMotionEvent*)xmotion_;
97  XWindowAttributes attrs;
98  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
99  int height = attrs.height;
100  int _items = height/(height/25);
101  int prelight_item = xmotion->y/_items;
102  if(prelight_item != filelist->prelight_item) {
103  filelist->prelight_item = prelight_item;
104  expose_widget(w);
105  }
106 }
107 
108 void _list_key_pressed(void *w_, void* xkey_, void* user_data) {
109  Widget_t *w = (Widget_t*)w_;
110  XKeyEvent *xkey = (XKeyEvent*)xkey_;
111  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
112  XWindowAttributes attrs;
113  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
114  int height = attrs.height;
115  int _items = height/(height/25);
116  // filelist->prelight_item = xkey->y/_items;
117  int nk = key_mapping(w->app->dpy, xkey);
118  if (nk) {
119  switch (nk) {
120  case 3: filelist->prelight_item = (xkey->y+24)/_items;
121  break;
122  case 4: filelist->prelight_item = (xkey->y+24)/_items;
123  break;
124  case 5: filelist->prelight_item = (xkey->y-24)/_items;
125  break;
126  case 6: filelist->prelight_item = (xkey->y-24)/_items;
127  break;
128  default:
129  break;
130  }
131  }
132 }
133 
134 void _list_entry_released(void *w_, void* button_, void* user_data) {
135  Widget_t *w = (Widget_t*)w_;
136  if (w->flags & HAS_POINTER) {
137  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
138  XButtonEvent *xbutton = (XButtonEvent*)button_;
139  XWindowAttributes attrs;
140  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
141  int height = attrs.height;
142  int _items = height/(height/25);
143  int prelight_item = xbutton->y/_items;
144  if(xbutton->button == Button4) {
145  prelight_item = (xbutton->y-24)/_items;
146  if(prelight_item != filelist->prelight_item) {
147  filelist->prelight_item = prelight_item;
148  }
149  } else if (xbutton->button == Button5) {
150  prelight_item = (xbutton->y+24)/_items;
151  if(prelight_item != filelist->prelight_item) {
152  filelist->prelight_item = prelight_item;
153  }
154  } else if(xbutton->button == Button1) {
155  Widget_t* listview = (Widget_t*) w->parent;
156  filelist->active_item = filelist->prelight_item;
157  adj_set_value(listview->adj,filelist->active_item);
158  }
159  }
160 }
161 
162 void _leave_list(void *w_, void* user_data) {
163  Widget_t *w = (Widget_t*)w_;
164  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
165  filelist->prelight_item = -1;
166  expose_widget(w);
167 }
168 
169 void _reconfigure_listview_viewport(void *w_, void* user_data) {
170  Widget_t *w = (Widget_t*)w_;
171  float st = adj_get_state(w->adj);
172  Widget_t* listview = (Widget_t*) w->parent;
173  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
174  XWindowAttributes attrs;
175  XGetWindowAttributes(listview->app->dpy, (Window)listview->widget, &attrs);
176  int height = attrs.height;
177  filelist->show_items = height/25;
178  w->adj->max_value = filelist->list_size-filelist->show_items;
179  adj_set_state(w->adj,st);
180 }
181 
182 void _configure_listview(void *w_, void* user_data) {
183  Widget_t *w = (Widget_t*)w_;
184  Widget_t* listview = (Widget_t*) w->parent;
185  ViewList_t *filelist = (ViewList_t*)w->parent_struct;
186  XWindowAttributes attrs;
187  XGetWindowAttributes(listview->app->dpy, (Window)listview->widget, &attrs);
188  int width = attrs.width;
189  XResizeWindow (w->app->dpy, w->widget, width, 25*(max(1,filelist->list_size)));
190 }
191 
192 void _draw_listview_viewslider(void *w_, void* user_data) {
193  Widget_t *w = (Widget_t*)w_;
194  int v = (int)w->adj->max_value;
195  if (v<=0) return;
196  XWindowAttributes attrs;
197  XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
198  if (attrs.map_state != IsViewable) return;
199  int width = attrs.width;
200  int height = attrs.height;
201  float sliderstate = adj_get_state(w->adj);
203  cairo_rectangle(w->crb, width-5,0,5,height);
204  cairo_fill_preserve(w->crb);
206  cairo_fill(w->crb);
208  cairo_rectangle(w->crb, width-5,(height-10)*sliderstate,5,10);
209  cairo_fill_preserve(w->crb);
211  cairo_set_line_width(w->crb,1);
212  cairo_stroke(w->crb);
213 }
214 
215 void _set_listview_viewpoint(void *w_, void* user_data) {
216  Widget_t *w = (Widget_t*)w_;
217  int v = (int)max(0,adj_get_value(w->adj));
218  XMoveWindow(w->app->dpy,w->widget,0, -25*v);
219 }
220 
_leave_list
void _leave_list(void *w_, void *user_data)
_leave_list - draw list on pointer leave
Definition: xlistview_private.c:162
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
XColor_t::active
Colors active
Definition: xcolor.h:92
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
ViewList_t::prelight_item
int prelight_item
Definition: xlistview.h:42
hide_tooltip
void hide_tooltip(Widget_t *wid)
hide_tooltip - check if a Widget_t have a tooltip, and hide it, if a tooltip is mapped.
Definition: xwidget.c:446
_configure_listview
void _configure_listview(void *w_, void *user_data)
_configure_listview - configure the viewport on mapping
Definition: xlistview_private.c:182
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
key_mapping
int key_mapping(Display *dpy, XKeyEvent *xkey)
_key_mapping - modifier key's mapped to a integer value
Definition: xwidget.c:26
Widget_t::parent_struct
void * parent_struct
Definition: xwidget.h:306
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
_list_motion
void _list_motion(void *w_, void *xmotion_, void *user_data)
_list_motion - follow mouse pointer to set prelight item
Definition: xlistview_private.c:93
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
adj_set_value
void adj_set_value(Adjustment_t *adj, float value)
adj_set_value - set the current value to the Adjustment_t
Definition: xadjustment.c:163
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::cr
cairo_t * cr
Definition: xwidget.h:314
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
BACKGROUND_
@ BACKGROUND_
Definition: xcolor.h:53
_draw_list
void _draw_list(void *w_, void *user_data)
_draw_list - draw list on expose call
Definition: xlistview_private.c:33
_reconfigure_listview_viewport
void _reconfigure_listview_viewport(void *w_, void *user_data)
_reconfigure_listview_viewport - reconfigure the viewport adjustment on size changes
Definition: xlistview_private.c:169
_list_entry_released
void _list_entry_released(void *w_, void *button_, void *user_data)
_list_entry_released - move listview or set active entry
Definition: xlistview_private.c:134
Widget_t::app
Xputty * app
Definition: xwidget.h:300
_list_key_pressed
void _list_key_pressed(void *w_, void *xkey_, void *user_data)
_list_key_pressed - move listview or set active entry
Definition: xlistview_private.c:108
Xputty::dpy
Display * dpy
Definition: xputty.h:181
ViewList_t::show_items
int show_items
Definition: xlistview.h:44
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
ACTIVE_
@ ACTIVE_
Definition: xcolor.h:42
Widget_t::widget
Window widget
Definition: xwidget.h:302
_draw_listview_viewslider
void _draw_listview_viewslider(void *w_, void *user_data)
_draw_listview_viewslider - draw a slider on the viewport to indicate the view point
Definition: xlistview_private.c:192
xlistview_private.h
show_tooltip
void show_tooltip(Widget_t *wid)
show_tooltip - check if a Widget_t have a tooltip, and show it, if a tooltip is available.
Definition: xwidget.c:427
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
NORMAL_
@ NORMAL_
Definition: xcolor.h:39
ViewList_t::list_size
int list_size
Definition: xlistview.h:45
PRELIGHT_
@ PRELIGHT_
Definition: xcolor.h:40
Adjustment_t::max_value
float max_value
Definition: xadjustment.h:90
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
set_pattern
void set_pattern(Widget_t *w, Colors *from, Colors *to, Color_mod mod)
set_pattern - set pattern for the selected Colors
Definition: xcolor.c:227
_draw_listview
void _draw_listview(void *w_, void *user_data)
_draw_listview - draw listview on expose call
Definition: xlistview_private.c:26
ViewList_t::list_names
char ** list_names
Definition: xlistview.h:46
Xputty::color_scheme
XColor_t * color_scheme
Definition: xputty.h:183
_set_listview_viewpoint
void _set_listview_viewpoint(void *w_, void *user_data)
_set_listview_viewpoint - move the view_port to position
Definition: xlistview_private.c:215
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
ViewList_t
ViewList_t - struct to hold information for the listview.
Definition: xlistview.h:41
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
xtooltip.h
SELECTED_
@ SELECTED_
Definition: xcolor.h:41
XColor_t::normal
Colors normal
Definition: xcolor.h:89
ViewList_t::active_item
int active_item
Definition: xlistview.h:43