rofi  1.5.1
scrollbar.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 #include <config.h>
29 #include <xkbcommon/xkbcommon.h>
30 #include <glib.h>
31 #include "widgets/textbox.h"
32 #include "widgets/listview.h"
33 #include "widgets/scrollbar.h"
34 
35 #include "theme.h"
36 
37 #define DEFAULT_SCROLLBAR_WIDTH 8
38 
39 static void scrollbar_draw ( widget *, cairo_t * );
40 static void scrollbar_free ( widget * );
41 
43 {
44  // Want height we are.
45  return wid->h;
46 }
47 
48 // TODO
49 // This should behave more like a real scrollbar.
50 guint scrollbar_scroll_get_line ( const scrollbar *sb, int y )
51 {
52  y -= sb->widget.border.top.distance;
53  if ( y < 0 ) {
54  return 0;
55  }
56 
57  if ( y > sb->widget.h ) {
58  return sb->length - 1;
59  }
60 
61  short r = ( sb->length * sb->widget.h ) / ( (double) ( sb->length + sb->pos_length ) );
62  short handle = sb->widget.h - r;
63  double sec = ( ( r ) / (double) ( sb->length - 1 ) );
64  short half_handle = handle / 2;
65  y -= half_handle;
66  y = MIN ( MAX ( 0, y ), sb->widget.h - 2 * half_handle );
67 
68  unsigned int sel = ( ( y ) / sec );
69  return MIN ( sel, sb->length - 1 );
70 }
71 
72 static void scrollbar_scroll ( scrollbar *sb, int y )
73 {
75 }
76 
77 static WidgetTriggerActionResult scrollbar_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, G_GNUC_UNUSED gint x, gint y, G_GNUC_UNUSED void *user_data )
78 {
79  scrollbar *sb = (scrollbar *) wid;
80  switch ( action )
81  {
82  case MOUSE_CLICK_DOWN:
84  case MOUSE_CLICK_UP:
85  scrollbar_scroll ( sb, y );
87  case MOUSE_DCLICK_DOWN:
88  case MOUSE_DCLICK_UP:
89  break;
90  }
91  return FALSE;
92 }
93 
94 static gboolean scrollbar_motion_notify ( widget *wid, G_GNUC_UNUSED gint x, gint y )
95 {
96  scrollbar *sb = (scrollbar *) wid;
97  scrollbar_scroll ( sb, y );
98  return TRUE;
99 }
100 
101 scrollbar *scrollbar_create ( widget *parent, const char *name )
102 {
103  scrollbar *sb = g_malloc0 ( sizeof ( scrollbar ) );
104  widget_init ( WIDGET ( sb ), parent, WIDGET_TYPE_SCROLLBAR, name );
105  sb->widget.x = 0;
106  sb->widget.y = 0;
107  sb->width = rofi_theme_get_distance ( WIDGET ( sb ), "handle-width", DEFAULT_SCROLLBAR_WIDTH );
109  sb->widget.w = widget_padding_get_padding_width ( WIDGET ( sb ) ) + width;
111 
112  sb->widget.draw = scrollbar_draw;
113  sb->widget.free = scrollbar_free;
117 
118  sb->length = 10;
119  sb->pos = 0;
120  sb->pos_length = 4;
121 
122  return sb;
123 }
124 
125 static void scrollbar_free ( widget *wid )
126 {
127  scrollbar *sb = (scrollbar *) wid;
128  g_free ( sb );
129 }
130 
131 void scrollbar_set_max_value ( scrollbar *sb, unsigned int max )
132 {
133  if ( sb != NULL ) {
134  sb->length = MAX ( 1u, max );
135  }
136 }
137 
138 void scrollbar_set_handle ( scrollbar *sb, unsigned int pos )
139 {
140  if ( sb != NULL ) {
141  sb->pos = MIN ( sb->length, pos );
142  }
143 }
144 
145 void scrollbar_set_handle_length ( scrollbar *sb, unsigned int pos_length )
146 {
147  if ( sb != NULL ) {
148  sb->pos_length = MIN ( sb->length, MAX ( 1u, pos_length ) );
149  }
150 }
151 
164 static void scrollbar_draw ( widget *wid, cairo_t *draw )
165 {
166  scrollbar *sb = (scrollbar *) wid;
167  unsigned int wh = widget_padding_get_remaining_height ( wid );
168  // Calculate position and size.
169  unsigned int r = ( sb->length * wh ) / ( (double) ( sb->length + sb->pos_length ) );
170  unsigned int handle = wid->h - r;
171  double sec = ( ( r ) / (double) ( sb->length - 1 ) );
172  unsigned int height = handle;
173  unsigned int y = sb->pos * sec;
174  // Set max pos.
175  y = MIN ( y, wh - handle );
176  // Never go out of bar.
177  height = MAX ( 2, height );
178  // Cap length;
179  rofi_theme_get_color ( WIDGET ( sb ), "handle-color", draw );
180 
181  cairo_rectangle ( draw,
182  widget_padding_get_left ( wid ),
183  widget_padding_get_top ( wid ) + y,
185  height );
186  cairo_fill ( draw );
187 }
void listview_set_selected(listview *lv, unsigned int selected)
Definition: listview.c:401
static WidgetTriggerActionResult scrollbar_trigger_action(widget *wid, MouseBindingMouseDefaultAction action, G_GNUC_UNUSED gint x, gint y, G_GNUC_UNUSED void *user_data)
Definition: scrollbar.c:77
MouseBindingMouseDefaultAction
Definition: keyb.h:154
double distance
Definition: rofi-types.h:95
void rofi_theme_get_color(const widget *widget, const char *property, cairo_t *d)
Definition: theme.c:647
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:548
int widget_padding_get_top(const widget *wid)
Definition: widget.c:506
unsigned int pos_length
Definition: scrollbar.h:48
struct _widget * parent
void(* draw)(struct _widget *widget, cairo_t *draw)
RofiDistance rofi_theme_get_distance(const widget *widget, const char *property, int def)
Definition: theme.c:549
#define DEFAULT_SCROLLBAR_WIDTH
Definition: scrollbar.c:37
static int scrollbar_get_desired_height(widget *wid)
Definition: scrollbar.c:42
unsigned int length
Definition: scrollbar.h:46
scrollbar * scrollbar_create(widget *parent, const char *name)
Definition: scrollbar.c:101
void scrollbar_set_handle(scrollbar *sb, unsigned int pos)
Definition: scrollbar.c:138
guint scrollbar_scroll_get_line(const scrollbar *sb, int y)
Definition: scrollbar.c:50
widget_trigger_action_cb trigger_action
widget widget
Definition: scrollbar.h:45
void scrollbar_set_handle_length(scrollbar *sb, unsigned int pos_length)
Definition: scrollbar.c:145
static gboolean scrollbar_motion_notify(widget *wid, G_GNUC_UNUSED gint x, gint y)
Definition: scrollbar.c:94
int widget_padding_get_remaining_height(const widget *wid)
Definition: widget.c:534
void(* free)(struct _widget *widget)
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition: theme.c:735
RofiDistance width
Definition: scrollbar.h:49
unsigned int pos
Definition: scrollbar.h:47
WidgetTriggerActionResult
Definition: widget.h:77
#define WIDGET(a)
Definition: widget.h:115
void scrollbar_set_max_value(scrollbar *sb, unsigned int max)
Definition: scrollbar.c:131
int widget_padding_get_remaining_width(const widget *wid)
Definition: widget.c:527
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:37
static void scrollbar_draw(widget *, cairo_t *)
Definition: scrollbar.c:164
static void scrollbar_free(widget *)
Definition: scrollbar.c:125
int(* get_desired_height)(struct _widget *)
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:541
RofiPadding border
static void scrollbar_scroll(scrollbar *sb, int y)
Definition: scrollbar.c:72
int widget_padding_get_left(const widget *wid)
Definition: widget.c:486
RofiDistance top
Definition: rofi-types.h:131
gboolean(* motion_notify)(struct _widget *, gint x, gint y)