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.h"
23 #include "xlistview_private.h"
24 #include "xtooltip.h"
25 #include "xslider.h"
26 
27 
listview_set_active_entry(Widget_t * w,int active)28 void listview_set_active_entry(Widget_t *w, int active) {
29     if(active<0) return;
30     Widget_t* view_port =  w->childlist->childs[0];
31     ViewList_t *filelist = (ViewList_t*)view_port->parent_struct;
32     float value = (float)active;
33     if (value>w->adj->max_value) value = w->adj->max_value;
34     else if (value<w->adj->min_value) value = w->adj->min_value;
35     filelist->active_item = (int)value;
36     adj_set_value(w->adj,filelist->active_item);
37 }
38 
listview_unset_active_entry(Widget_t * w)39 void listview_unset_active_entry(Widget_t *w) {
40     Widget_t* view_port =  w->childlist->childs[0];
41     ViewList_t *filelist = (ViewList_t*)view_port->parent_struct;
42     filelist->active_item = -1;
43     filelist->prelight_item = -1;
44 }
45 
create_listview_viewport(Widget_t * parent,int elem,int width,int height)46 Widget_t* create_listview_viewport(Widget_t *parent, int elem, int width, int height) {
47     Widget_t *wid = create_widget(parent->app, parent, 0, 0, width, height);
48     XSelectInput(wid->app->dpy, wid->widget,StructureNotifyMask|ExposureMask|KeyPressMask
49                     |EnterWindowMask|LeaveWindowMask|ButtonReleaseMask
50                     |ButtonPressMask|Button1MotionMask|PointerMotionMask);
51     wid->scale.gravity = NORTHWEST;
52     ViewList_t *filelist;
53     filelist = (ViewList_t*)malloc(sizeof(ViewList_t));
54     filelist->show_items = elem;
55     filelist->check_dir = 0;
56     wid->flags |= HAS_MEM;
57     wid->parent_struct = filelist;
58     float max_value = -elem;
59     wid->adj_y = add_adjustment(wid,0.0, 0.0, 0.0, max_value,1.0, CL_VIEWPORT);
60     wid->adj = wid->adj_y;
61     wid->func.adj_callback = _set_listview_viewpoint;
62     wid->func.motion_callback = _list_motion;
63     wid->func.leave_callback = _leave_list;
64     wid->func.button_release_callback = _list_entry_released;
65     wid->func.double_click_callback = _list_entry_double_clicked;
66     wid->func.key_press_callback = _list_key_pressed;
67     wid->func.expose_callback = _draw_list;
68     wid->func.configure_notify_callback = _reconfigure_listview_viewport;
69     wid->func.map_notify_callback = _configure_listview;
70     wid->func.mem_free_callback = listview_mem_free;
71     return wid;
72 }
73 
add_listview(Widget_t * parent,const char * label,int x,int y,int width,int height)74 Widget_t* add_listview(Widget_t *parent, const char * label,
75                 int x, int y, int width, int height) {
76 
77     Widget_t *wid = create_widget(parent->app, parent, x, y, width, height);
78     wid->label = label;
79     wid->scale.gravity = CENTER;
80     wid->flags &= ~USE_TRANSPARENCY;
81     wid->func.expose_callback = _draw_listview;
82     int elem = height/25;
83     wid->adj_y = add_adjustment(wid,0.0, 0.0, 0.0, -1.0,1.0, CL_NONE);
84     wid->adj = wid->adj_y;
85     Widget_t *viewport = create_listview_viewport(wid, elem, width-10, height);
86 
87     ViewList_t *filelist = (ViewList_t*)viewport->parent_struct;
88     filelist->folder = surface_get_png(wid, filelist->folder, LDVAR(directory_png));
89     filelist->file = surface_get_png(wid, filelist->folder, LDVAR(file_png));
90     filelist->slider = add_vslider(wid, "", width-10, 0, 10, height);
91     filelist->slider->func.expose_callback = _draw_listviewslider;
92     filelist->slider->adj_y = add_adjustment(filelist->slider,0.0, 0.0, 0.0, 1.0,0.0085, CL_VIEWPORTSLIDER);
93     filelist->slider->adj = filelist->slider->adj_y;
94     filelist->slider->func.value_changed_callback = _set_listviewport;
95     filelist->slider->scale.gravity = WESTSOUTH;
96     filelist->slider->flags &= ~USE_TRANSPARENCY;
97     filelist->slider->flags |= NO_AUTOREPEAT | NO_PROPAGATE;
98     filelist->slider->parent_struct = viewport;
99 
100     return wid;
101 }
102 
listview_mem_free(void * w_,void * user_data)103 void listview_mem_free(void *w_, void* user_data) {
104     Widget_t *w = (Widget_t*)w_;
105     ViewList_t *filelist = (ViewList_t*)w->parent_struct;
106     cairo_surface_destroy(filelist->folder);
107     cairo_surface_destroy(filelist->file);
108     free(filelist);
109 }
110 
listview_remove_list(Widget_t * listview)111 void listview_remove_list(Widget_t *listview) {
112     Widget_t* view_port =  listview->childlist->childs[0];
113     ViewList_t *filelist = (ViewList_t*)view_port->parent_struct;
114     filelist->list_names = NULL;
115     filelist->list_size = 0;
116     XWindowAttributes attrs;
117     XGetWindowAttributes(listview->app->dpy, (Window)listview->widget, &attrs);
118     int height = attrs.height;
119     float elem = height/25;
120     set_adjustment(listview->adj,0.0, 0.0, 0.0, -1.0,1.0, CL_NONE);
121     set_adjustment(view_port->adj,0.0, 0.0, 0.0, -elem,1.0, CL_VIEWPORT);
122     adj_set_value(filelist->slider->adj,0.0);
123 }
124 
listview_set_list(Widget_t * listview,char ** list,int list_size)125 void listview_set_list(Widget_t *listview, char **list, int list_size) {
126     Widget_t* view_port =  listview->childlist->childs[0];
127     ViewList_t *filelist = (ViewList_t*)view_port->parent_struct;
128     filelist->list_names = list;
129     filelist->list_size = list_size;
130     set_adjustment(listview->adj,0.0, 0.0, 0.0, (float)(list_size-1.0),1.0, CL_NONE);
131     float max_value = view_port->adj->max_value+ (float)list_size;
132     set_adjustment(view_port->adj,0.0, 0.0, 0.0, max_value,1.0, CL_VIEWPORT);
133     _configure_listview(view_port, NULL);
134 }
135 
listview_set_check_dir(Widget_t * listview,int set)136 void listview_set_check_dir(Widget_t *listview, int set) {
137     Widget_t* view_port =  listview->childlist->childs[0];
138     ViewList_t *filelist = (ViewList_t*)view_port->parent_struct;
139     filelist->check_dir = set;
140 }
141