2017-04-15 10:32:05 +00:00
|
|
|
/*
|
|
|
|
* rofi
|
2016-10-08 16:57:59 +00:00
|
|
|
*
|
2017-04-15 10:32:05 +00:00
|
|
|
* MIT/X11 License
|
2020-01-01 11:23:12 +00:00
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2016-10-08 16:57:59 +00:00
|
|
|
*
|
2017-04-15 10:32:05 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2016-10-08 16:57:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <widgets/widget.h>
|
|
|
|
#include <widgets/textbox.h>
|
|
|
|
#include <widgets/scrollbar.h>
|
2019-08-08 18:02:20 +00:00
|
|
|
#include <widgets/icon.h>
|
|
|
|
#include <widgets/box.h>
|
|
|
|
#include <widgets/listview.h>
|
2016-10-08 16:57:59 +00:00
|
|
|
|
2016-12-11 16:06:31 +00:00
|
|
|
#include "settings.h"
|
|
|
|
#include "theme.h"
|
|
|
|
|
2019-12-29 20:26:41 +00:00
|
|
|
#include "timings.h"
|
|
|
|
|
2018-06-12 12:11:08 +00:00
|
|
|
/** Default spacing between the elements in the listview. */
|
2017-01-08 20:36:06 +00:00
|
|
|
#define DEFAULT_SPACING 2
|
2017-01-01 14:39:02 +00:00
|
|
|
|
2018-06-12 12:11:08 +00:00
|
|
|
/**
|
|
|
|
* Orientation of the listview
|
|
|
|
*/
|
2020-08-28 14:41:59 +00:00
|
|
|
/** Vertical (classical) list */
|
|
|
|
#define LISTVIEW ROFI_ORIENTATION_VERTICAL
|
|
|
|
/** Horizontal list. (barview) */
|
|
|
|
#define BARVIEW ROFI_ORIENTATION_HORIZONTAL
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2018-06-12 12:11:08 +00:00
|
|
|
/**
|
|
|
|
* The moving direction of the selection, this (in barview) affects the scrolling.
|
|
|
|
*/
|
2017-06-03 18:35:50 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
LEFT_TO_RIGHT = 0,
|
|
|
|
RIGHT_TO_LEFT = 1
|
2017-05-23 07:45:42 +00:00
|
|
|
} MoveDirection;
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
box *box;
|
|
|
|
textbox *textbox;
|
|
|
|
textbox *index;
|
|
|
|
icon *icon;
|
2019-08-08 18:02:20 +00:00
|
|
|
} _listview_row;
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
struct _listview
|
|
|
|
{
|
2020-08-28 14:41:59 +00:00
|
|
|
widget widget;
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2020-04-26 09:43:14 +00:00
|
|
|
RofiOrientation type;
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
// RChanged
|
|
|
|
// Text needs to be repainted.
|
|
|
|
unsigned int rchanged;
|
|
|
|
// Administration
|
|
|
|
|
|
|
|
unsigned int cur_page;
|
|
|
|
unsigned int last_offset;
|
|
|
|
unsigned int selected;
|
|
|
|
|
|
|
|
unsigned int element_height;
|
|
|
|
unsigned int max_rows;
|
|
|
|
unsigned int max_elements;
|
|
|
|
|
|
|
|
//
|
2019-08-08 18:02:20 +00:00
|
|
|
gboolean fixed_columns;
|
2016-10-08 16:57:59 +00:00
|
|
|
unsigned int cur_columns;
|
|
|
|
unsigned int req_elements;
|
|
|
|
unsigned int cur_elements;
|
|
|
|
|
2017-06-02 14:25:47 +00:00
|
|
|
RofiDistance spacing;
|
2016-10-08 16:57:59 +00:00
|
|
|
unsigned int menu_lines;
|
2017-01-02 20:19:27 +00:00
|
|
|
unsigned int max_displayed_lines;
|
2016-10-08 16:57:59 +00:00
|
|
|
unsigned int menu_columns;
|
|
|
|
unsigned int fixed_num_lines;
|
2017-01-03 13:25:24 +00:00
|
|
|
unsigned int dynamic;
|
2016-12-28 12:46:53 +00:00
|
|
|
unsigned int eh;
|
2017-01-03 19:22:29 +00:00
|
|
|
unsigned int reverse;
|
2016-10-08 16:57:59 +00:00
|
|
|
gboolean cycle;
|
|
|
|
gboolean multi_select;
|
|
|
|
|
|
|
|
ScrollType scroll_type;
|
|
|
|
|
2019-08-08 18:02:20 +00:00
|
|
|
_listview_row * boxes;
|
2016-10-08 16:57:59 +00:00
|
|
|
scrollbar *scrollbar;
|
|
|
|
|
|
|
|
listview_update_callback callback;
|
|
|
|
void *udata;
|
|
|
|
|
2016-10-25 19:19:39 +00:00
|
|
|
gboolean scrollbar_scroll;
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
xcb_timestamp_t last_click;
|
|
|
|
listview_mouse_activated_cb mouse_activated;
|
|
|
|
void *mouse_activated_data;
|
2017-01-08 15:09:24 +00:00
|
|
|
|
2017-01-08 20:36:06 +00:00
|
|
|
char *listview_name;
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2019-05-04 09:01:09 +00:00
|
|
|
PangoEllipsizeMode emode;
|
2017-05-23 07:45:42 +00:00
|
|
|
/** Barview */
|
2017-06-03 18:35:50 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
MoveDirection direction;
|
|
|
|
unsigned int cur_visible;
|
2017-05-23 08:48:02 +00:00
|
|
|
} barview;
|
2016-10-08 16:57:59 +00:00
|
|
|
};
|
2019-09-30 08:35:12 +00:00
|
|
|
/**
|
|
|
|
* Names used for theming the elements in the listview.
|
|
|
|
* Each row can have 3 modes, normal, selected and alternate.
|
|
|
|
* Each row can have 3 states, normal, urgent and active.
|
|
|
|
*/
|
2019-08-08 18:02:20 +00:00
|
|
|
const char *const listview_theme_prop_names[][3] = {
|
|
|
|
/** Normal row */
|
|
|
|
{ "normal.normal", "selected.normal", "alternate.normal" },
|
|
|
|
/** Urgent row */
|
|
|
|
{ "normal.urgent", "selected.urgent", "alternate.urgent" },
|
|
|
|
/** Active row */
|
|
|
|
{ "normal.active", "selected.active", "alternate.active" },
|
|
|
|
};
|
|
|
|
|
2020-05-13 14:25:12 +00:00
|
|
|
static void listview_set_state ( _listview_row r, TextBoxFontType tbft )
|
2019-08-08 18:02:20 +00:00
|
|
|
{
|
2020-08-28 14:41:59 +00:00
|
|
|
widget *w = WIDGET ( r.box );
|
|
|
|
TextBoxFontType t = tbft & STATE_MASK;
|
2019-08-08 18:02:20 +00:00
|
|
|
if ( w == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// ACTIVE has priority over URGENT if both set.
|
|
|
|
if ( t == ( URGENT | ACTIVE ) ) {
|
|
|
|
t = ACTIVE;
|
|
|
|
}
|
|
|
|
switch ( ( tbft & FMOD_MASK ) )
|
|
|
|
{
|
|
|
|
case HIGHLIGHT:
|
|
|
|
widget_set_state ( w, listview_theme_prop_names[t][1] );
|
|
|
|
break;
|
|
|
|
case ALT:
|
|
|
|
widget_set_state ( w, listview_theme_prop_names[t][2] );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
widget_set_state ( w, listview_theme_prop_names[t][0] );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 15:17:15 +00:00
|
|
|
static void listview_add_widget ( listview *lv, _listview_row *row, widget *wid, const char *label )
|
|
|
|
{
|
|
|
|
TextboxFlags flags = ( lv->multi_select ) ? TB_INDICATOR : 0;
|
|
|
|
if ( strcasecmp ( label, "element-icon" ) == 0 ) {
|
|
|
|
if ( config.show_icons ) {
|
|
|
|
row->icon = icon_create ( WIDGET ( wid ), "element-icon" );
|
2020-08-28 14:41:59 +00:00
|
|
|
box_add ( (box *) wid, WIDGET ( row->icon ), FALSE );
|
2020-05-13 15:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( strcasecmp ( label, "element-text" ) == 0 ) {
|
|
|
|
row->textbox = textbox_create ( WIDGET ( wid ), WIDGET_TYPE_TEXTBOX_TEXT, "element-text", TB_AUTOHEIGHT | flags, NORMAL, "DDD", 0, 0 );
|
2020-08-28 14:41:59 +00:00
|
|
|
box_add ( (box *) wid, WIDGET ( row->textbox ), TRUE );
|
2020-05-13 15:17:15 +00:00
|
|
|
}
|
|
|
|
else if ( strcasecmp ( label, "element-index" ) == 0 ) {
|
|
|
|
row->index = textbox_create ( WIDGET ( wid ), WIDGET_TYPE_TEXTBOX_TEXT, "element-text", TB_AUTOHEIGHT, NORMAL, " ", 0, 0 );
|
2020-08-28 14:41:59 +00:00
|
|
|
box_add ( (box *) wid, WIDGET ( row->index ), FALSE );
|
|
|
|
}
|
|
|
|
else {
|
2020-05-13 15:17:15 +00:00
|
|
|
widget *wid2 = (widget *) box_create ( wid, label, ROFI_ORIENTATION_VERTICAL );
|
|
|
|
box_add ( (box *) wid, WIDGET ( wid2 ), TRUE );
|
2020-08-28 14:41:59 +00:00
|
|
|
GList *list = rofi_theme_get_list ( WIDGET ( wid2 ), "children", "" );
|
2020-05-13 15:17:15 +00:00
|
|
|
for ( GList *iter = g_list_first ( list ); iter != NULL; iter = g_list_next ( iter ) ) {
|
2020-08-28 14:41:59 +00:00
|
|
|
listview_add_widget ( lv, row, wid2, (const char *) iter->data );
|
2020-05-13 15:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 14:25:12 +00:00
|
|
|
|
2019-08-08 18:02:20 +00:00
|
|
|
static void listview_create_row ( listview *lv, _listview_row *row )
|
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
row->box = box_create ( WIDGET ( lv ), "element", ROFI_ORIENTATION_HORIZONTAL );
|
|
|
|
widget_set_type ( WIDGET ( row->box ), WIDGET_TYPE_LISTVIEW_ELEMENT );
|
|
|
|
GList *list = rofi_theme_get_list ( WIDGET ( row->box ), "children", "element-icon,element-text" );
|
2019-08-08 18:02:20 +00:00
|
|
|
|
|
|
|
row->textbox = NULL;
|
|
|
|
row->icon = NULL;
|
2019-12-29 18:04:29 +00:00
|
|
|
row->index = NULL;
|
2019-08-08 18:02:20 +00:00
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( GList *iter = g_list_first ( list ); iter != NULL; iter = g_list_next ( iter ) ) {
|
2020-08-28 14:41:59 +00:00
|
|
|
listview_add_widget ( lv, row, WIDGET ( row->box ), (const char *) iter->data );
|
2019-08-08 18:02:20 +00:00
|
|
|
}
|
|
|
|
g_list_free_full ( list, g_free );
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:00:06 +00:00
|
|
|
static int listview_get_desired_height ( widget *wid );
|
|
|
|
|
2016-10-18 11:49:24 +00:00
|
|
|
static void listview_free ( widget *wid )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2016-10-18 11:49:24 +00:00
|
|
|
listview *lv = (listview *) wid;
|
|
|
|
for ( unsigned int i = 0; i < lv->cur_elements; i++ ) {
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_free ( WIDGET ( lv->boxes [i].box ) );
|
2016-10-18 11:49:24 +00:00
|
|
|
}
|
|
|
|
g_free ( lv->boxes );
|
|
|
|
|
2017-01-08 20:36:06 +00:00
|
|
|
g_free ( lv->listview_name );
|
2016-10-18 11:49:24 +00:00
|
|
|
widget_free ( WIDGET ( lv->scrollbar ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
g_free ( lv );
|
|
|
|
}
|
2017-05-23 08:48:02 +00:00
|
|
|
static unsigned int scroll_per_page_barview ( listview *lv )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2017-05-23 08:48:02 +00:00
|
|
|
unsigned int offset = lv->last_offset;
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2017-05-23 08:48:02 +00:00
|
|
|
// selected row is always visible.
|
|
|
|
// If selected is visible do not scroll.
|
|
|
|
if ( lv->selected < lv->last_offset ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
offset = lv->selected;
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->rchanged = TRUE;
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else if ( lv->selected >= ( lv->last_offset + lv->barview.cur_visible ) ) {
|
|
|
|
offset = lv->selected;
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->rchanged = TRUE;
|
2017-05-23 07:45:42 +00:00
|
|
|
}
|
2017-05-23 08:48:02 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
static unsigned int scroll_per_page ( listview * lv )
|
|
|
|
{
|
|
|
|
int offset = 0;
|
2016-10-08 16:57:59 +00:00
|
|
|
|
|
|
|
// selected row is always visible.
|
|
|
|
// If selected is visible do not scroll.
|
|
|
|
if ( ( ( lv->selected - ( lv->last_offset ) ) < ( lv->max_elements ) ) && ( lv->selected >= ( lv->last_offset ) ) ) {
|
|
|
|
offset = lv->last_offset;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// Do paginating
|
|
|
|
unsigned int page = ( lv->max_elements > 0 ) ? ( lv->selected / lv->max_elements ) : 0;
|
|
|
|
offset = page * lv->max_elements;
|
|
|
|
if ( page != lv->cur_page ) {
|
|
|
|
lv->cur_page = page;
|
|
|
|
lv->rchanged = TRUE;
|
|
|
|
}
|
|
|
|
// Set the position
|
2016-10-25 19:19:39 +00:00
|
|
|
//scrollbar_set_handle ( lv->scrollbar, page * lv->max_elements );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int scroll_continious ( listview *lv )
|
|
|
|
{
|
|
|
|
unsigned int middle = ( lv->max_rows - ( ( lv->max_rows & 1 ) == 0 ) ) / 2;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
if ( lv->selected > middle ) {
|
|
|
|
if ( lv->selected < ( lv->req_elements - ( lv->max_rows - middle ) ) ) {
|
|
|
|
offset = lv->selected - middle;
|
|
|
|
}
|
|
|
|
// Don't go below zero.
|
|
|
|
else if ( lv->req_elements > lv->max_rows ) {
|
|
|
|
offset = lv->req_elements - lv->max_rows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( offset != lv->cur_page ) {
|
2016-10-25 19:19:39 +00:00
|
|
|
//scrollbar_set_handle ( lv->scrollbar, offset );
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->cur_page = offset;
|
|
|
|
lv->rchanged = TRUE;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_element ( listview *lv, unsigned int tb, unsigned int index, gboolean full )
|
|
|
|
{
|
|
|
|
// Select drawing mode
|
|
|
|
TextBoxFontType type = ( index & 1 ) == 0 ? NORMAL : ALT;
|
|
|
|
type = ( index ) == lv->selected ? HIGHLIGHT : type;
|
|
|
|
|
2019-12-29 18:04:29 +00:00
|
|
|
if ( lv->boxes[tb].index ) {
|
|
|
|
if ( index < 10 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
char str[2] = {
|
|
|
|
( ( index + 1 ) % 10 ) + '0',
|
2019-12-29 20:23:26 +00:00
|
|
|
'\0'
|
|
|
|
};
|
2020-02-02 12:56:37 +00:00
|
|
|
textbox_text ( lv->boxes[tb].index, str );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textbox_text ( lv->boxes[tb].index, " " );
|
2019-12-29 18:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( lv->callback ) {
|
2019-08-08 18:02:20 +00:00
|
|
|
lv->callback ( lv->boxes[tb].textbox, lv->boxes[tb].icon, index, lv->udata, &type, full );
|
2020-02-02 12:56:37 +00:00
|
|
|
listview_set_state ( lv->boxes[tb], type );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 07:45:42 +00:00
|
|
|
static void barview_draw ( widget *wid, cairo_t *draw )
|
|
|
|
{
|
|
|
|
unsigned int offset = 0;
|
2017-06-03 18:35:50 +00:00
|
|
|
listview *lv = (listview *) wid;
|
|
|
|
offset = scroll_per_page_barview ( lv );
|
2017-05-23 07:45:42 +00:00
|
|
|
lv->last_offset = offset;
|
2017-06-02 14:34:52 +00:00
|
|
|
int spacing_hori = distance_get_pixel ( lv->spacing, ROFI_ORIENTATION_HORIZONTAL );
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
int left_offset = widget_padding_get_left ( wid );
|
|
|
|
int right_offset = lv->widget.w - widget_padding_get_right ( wid );
|
|
|
|
int top_offset = widget_padding_get_top ( wid );
|
2017-05-23 07:45:42 +00:00
|
|
|
if ( lv->cur_elements > 0 ) {
|
2017-11-23 17:41:52 +00:00
|
|
|
// Set new x/y position.
|
2017-05-23 07:45:42 +00:00
|
|
|
unsigned int max = MIN ( lv->cur_elements, lv->req_elements - offset );
|
|
|
|
if ( lv->rchanged ) {
|
2017-05-23 08:48:02 +00:00
|
|
|
int first = TRUE;
|
2017-05-23 07:45:42 +00:00
|
|
|
int width = lv->widget.w;
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->barview.cur_visible = 0;
|
2017-06-03 18:35:50 +00:00
|
|
|
width -= widget_padding_get_padding_width ( wid );
|
2017-05-23 08:48:02 +00:00
|
|
|
if ( lv->barview.direction == LEFT_TO_RIGHT ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
for ( unsigned int i = 0; i < max && width > 0; i++ ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
update_element ( lv, i, i + offset, TRUE );
|
2020-07-05 09:41:04 +00:00
|
|
|
int twidth = widget_get_desired_width ( WIDGET ( lv->boxes[i].box ) );
|
2017-05-23 07:45:42 +00:00
|
|
|
if ( twidth >= width ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( !first ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
twidth = width;
|
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
widget_move ( WIDGET ( lv->boxes[i].box ), left_offset, top_offset );
|
|
|
|
widget_resize ( WIDGET ( lv->boxes[i].box ), twidth, lv->element_height );
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_draw ( WIDGET ( lv->boxes[i].box ), draw );
|
2017-06-03 18:35:50 +00:00
|
|
|
width -= twidth + spacing_hori;
|
2017-05-23 08:48:02 +00:00
|
|
|
left_offset += twidth + spacing_hori;
|
2017-06-03 18:35:50 +00:00
|
|
|
first = FALSE;
|
|
|
|
lv->barview.cur_visible++;
|
2017-05-23 07:45:42 +00:00
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for ( unsigned int i = 0; i < lv->cur_elements && width > 0 && i <= offset; i++ ) {
|
|
|
|
update_element ( lv, i, offset - i, TRUE );
|
2020-08-28 14:41:59 +00:00
|
|
|
int twidth = widget_get_desired_width ( WIDGET ( lv->boxes[i].box ) );
|
2017-05-23 07:45:42 +00:00
|
|
|
if ( twidth >= width ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( !first ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
twidth = width;
|
|
|
|
}
|
|
|
|
right_offset -= twidth;
|
2020-02-02 12:56:37 +00:00
|
|
|
widget_move ( WIDGET ( lv->boxes[i].box ), right_offset, top_offset );
|
|
|
|
widget_resize ( WIDGET ( lv->boxes[i].box ), twidth, lv->element_height );
|
2017-05-23 07:45:42 +00:00
|
|
|
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_draw ( WIDGET ( lv->boxes[i].box ), draw );
|
2017-06-03 18:35:50 +00:00
|
|
|
width -= twidth + spacing_hori;
|
2017-05-23 08:48:02 +00:00
|
|
|
right_offset -= spacing_hori;
|
2017-06-03 18:35:50 +00:00
|
|
|
first = FALSE;
|
|
|
|
lv->barview.cur_visible++;
|
2017-05-23 07:45:42 +00:00
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
offset -= lv->barview.cur_visible - 1;
|
2017-05-23 07:45:42 +00:00
|
|
|
lv->last_offset = offset;
|
2017-06-03 18:35:50 +00:00
|
|
|
for ( unsigned int i = 0; i < ( lv->barview.cur_visible / 2 ); i++ ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
_listview_row temp = lv->boxes[i];
|
|
|
|
int sw = lv->barview.cur_visible - i - 1;
|
2017-06-03 18:35:50 +00:00
|
|
|
lv->boxes[i] = lv->boxes[sw];
|
2017-05-23 07:45:42 +00:00
|
|
|
lv->boxes[sw] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lv->rchanged = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
2017-05-23 08:48:02 +00:00
|
|
|
for ( unsigned int i = 0; i < lv->barview.cur_visible; i++ ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
update_element ( lv, i, i + offset, TRUE );
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_draw ( WIDGET ( lv->boxes[i].box ), draw );
|
2017-05-23 07:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
static void listview_draw ( widget *wid, cairo_t *draw )
|
|
|
|
{
|
|
|
|
unsigned int offset = 0;
|
|
|
|
listview *lv = (listview *) wid;
|
|
|
|
if ( lv->scroll_type == LISTVIEW_SCROLL_CONTINIOUS ) {
|
|
|
|
offset = scroll_continious ( lv );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
offset = scroll_per_page ( lv );
|
|
|
|
}
|
2016-12-20 18:29:46 +00:00
|
|
|
// Set these all together to make sure they update consistently.
|
|
|
|
scrollbar_set_max_value ( lv->scrollbar, lv->req_elements );
|
|
|
|
scrollbar_set_handle_length ( lv->scrollbar, lv->cur_columns * lv->max_rows );
|
2017-01-03 19:28:19 +00:00
|
|
|
if ( lv->reverse ) {
|
2017-01-08 20:36:06 +00:00
|
|
|
scrollbar_set_handle ( lv->scrollbar, lv->req_elements - lv->selected - 1 );
|
|
|
|
}
|
|
|
|
else {
|
2017-01-03 19:28:19 +00:00
|
|
|
scrollbar_set_handle ( lv->scrollbar, lv->selected );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->last_offset = offset;
|
2017-06-02 14:34:52 +00:00
|
|
|
int spacing_vert = distance_get_pixel ( lv->spacing, ROFI_ORIENTATION_VERTICAL );
|
|
|
|
int spacing_hori = distance_get_pixel ( lv->spacing, ROFI_ORIENTATION_HORIZONTAL );
|
2017-01-06 15:41:23 +00:00
|
|
|
|
|
|
|
int left_offset = widget_padding_get_left ( wid );
|
2017-01-06 19:36:06 +00:00
|
|
|
int top_offset = widget_padding_get_top ( wid );
|
2017-06-02 14:53:39 +00:00
|
|
|
/*
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->scrollbar->widget.index == 0 ) {
|
2017-01-06 15:41:23 +00:00
|
|
|
left_offset += spacing_hori + lv->scrollbar->widget.w;
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
*/
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( lv->cur_elements > 0 && lv->max_rows > 0 ) {
|
2017-11-23 17:41:52 +00:00
|
|
|
// Set new x/y position.
|
2016-10-08 16:57:59 +00:00
|
|
|
unsigned int max = MIN ( lv->cur_elements, lv->req_elements - offset );
|
|
|
|
if ( lv->rchanged ) {
|
2020-01-01 14:44:01 +00:00
|
|
|
unsigned int width = lv->widget.w;
|
2016-12-29 16:04:16 +00:00
|
|
|
width -= widget_padding_get_padding_width ( wid );
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( widget_enabled ( WIDGET ( lv->scrollbar ) ) ) {
|
2017-01-03 18:23:09 +00:00
|
|
|
width -= spacing_hori;
|
2016-10-17 16:21:03 +00:00
|
|
|
width -= widget_get_width ( WIDGET ( lv->scrollbar ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
unsigned int element_width = ( width - spacing_hori * ( lv->cur_columns - 1 ) ) / lv->cur_columns;
|
|
|
|
|
|
|
|
int d = width - ( element_width + spacing_hori ) * ( lv->cur_columns - 1 ) - element_width;
|
|
|
|
if ( lv->cur_columns > 1 ) {
|
|
|
|
int diff = d / ( lv->cur_columns - 1 );
|
|
|
|
if ( diff >= 1 ) {
|
|
|
|
spacing_hori += 1;
|
|
|
|
d -= lv->cur_columns - 1;
|
2019-12-29 16:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
for ( unsigned int i = 0; i < max; i++ ) {
|
2017-01-06 15:41:23 +00:00
|
|
|
unsigned int ex = left_offset + ( ( i ) / lv->max_rows ) * ( element_width + spacing_hori );
|
2019-12-29 16:30:53 +00:00
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( ( i ) / lv->max_rows == ( lv->cur_columns - 1 ) ) {
|
|
|
|
ex += d;
|
2019-12-29 16:30:53 +00:00
|
|
|
}
|
2017-01-03 19:22:29 +00:00
|
|
|
if ( lv->reverse ) {
|
2017-01-08 20:36:06 +00:00
|
|
|
unsigned int ey = wid->h - ( widget_padding_get_bottom ( wid ) + ( ( i ) % lv->max_rows ) * ( lv->element_height + spacing_vert ) ) - lv->element_height;
|
2020-02-02 12:56:37 +00:00
|
|
|
widget_move ( WIDGET ( lv->boxes[i].box ), ex, ey );
|
|
|
|
widget_resize ( WIDGET ( lv->boxes[i].box ), element_width, lv->element_height );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-06 19:36:06 +00:00
|
|
|
unsigned int ey = top_offset + ( ( i ) % lv->max_rows ) * ( lv->element_height + spacing_vert );
|
2020-02-02 12:56:37 +00:00
|
|
|
widget_move ( WIDGET ( lv->boxes[i].box ), ex, ey );
|
|
|
|
widget_resize ( WIDGET ( lv->boxes[i].box ), element_width, lv->element_height );
|
2017-01-03 19:22:29 +00:00
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
|
|
|
|
update_element ( lv, i, i + offset, TRUE );
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_draw ( WIDGET ( lv->boxes[i].box ), draw );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
lv->rchanged = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for ( unsigned int i = 0; i < max; i++ ) {
|
2019-11-23 15:23:58 +00:00
|
|
|
update_element ( lv, i, i + offset, TRUE );
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_draw ( WIDGET ( lv->boxes[i].box ), draw );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 21:12:24 +00:00
|
|
|
widget_draw ( WIDGET ( lv->scrollbar ), draw );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2017-05-30 10:45:48 +00:00
|
|
|
static WidgetTriggerActionResult listview_element_trigger_action ( widget *wid, MouseBindingListviewElementAction action, gint x, gint y, void *user_data );
|
2017-05-28 00:18:09 +00:00
|
|
|
|
2017-05-23 07:45:42 +00:00
|
|
|
static void _listview_draw ( widget *wid, cairo_t *draw )
|
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
listview *lv = (listview *) wid;
|
|
|
|
if ( lv->type == LISTVIEW ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
listview_draw ( wid, draw );
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-23 07:45:42 +00:00
|
|
|
barview_draw ( wid, draw );
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 18:02:20 +00:00
|
|
|
/**
|
|
|
|
* State names used for theming.
|
|
|
|
*/
|
2016-10-08 16:57:59 +00:00
|
|
|
static void listview_recompute_elements ( listview *lv )
|
|
|
|
{
|
|
|
|
unsigned int newne = 0;
|
|
|
|
if ( lv->max_rows == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( !( lv->fixed_columns ) && lv->req_elements < lv->max_elements ) {
|
2016-10-08 16:57:59 +00:00
|
|
|
newne = lv->req_elements;
|
|
|
|
lv->cur_columns = ( lv->req_elements + ( lv->max_rows - 1 ) ) / lv->max_rows;
|
|
|
|
}
|
|
|
|
else {
|
2020-02-02 12:56:37 +00:00
|
|
|
newne = MIN ( lv->req_elements, lv->max_elements );
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->cur_columns = lv->menu_columns;
|
|
|
|
}
|
|
|
|
for ( unsigned int i = newne; i < lv->cur_elements; i++ ) {
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_free ( WIDGET ( lv->boxes[i].box ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2019-08-08 18:02:20 +00:00
|
|
|
lv->boxes = g_realloc ( lv->boxes, newne * sizeof ( _listview_row ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( newne > 0 ) {
|
|
|
|
for ( unsigned int i = lv->cur_elements; i < newne; i++ ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
listview_create_row ( lv, &( lv->boxes[i] ) );
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_set_trigger_action_handler ( WIDGET ( lv->boxes[i].box ), listview_element_trigger_action, lv );
|
2020-02-02 12:56:37 +00:00
|
|
|
listview_set_state ( lv->boxes[i], NORMAL );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
lv->rchanged = TRUE;
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->cur_elements = newne;
|
|
|
|
}
|
|
|
|
|
|
|
|
void listview_set_num_elements ( listview *lv, unsigned int rows )
|
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
TICK_N ( __FUNCTION__ );
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->req_elements = rows;
|
|
|
|
listview_set_selected ( lv, lv->selected );
|
2020-02-02 12:56:37 +00:00
|
|
|
TICK_N ( "Set selected" );
|
2016-10-08 16:57:59 +00:00
|
|
|
listview_recompute_elements ( lv );
|
2020-02-02 12:56:37 +00:00
|
|
|
TICK_N ( "recompute elements" );
|
2016-10-08 16:57:59 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
2020-02-02 12:56:37 +00:00
|
|
|
TICK_N ( "queue redraw" );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int listview_get_selected ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv != NULL ) {
|
|
|
|
return lv->selected;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void listview_set_selected ( listview *lv, unsigned int selected )
|
|
|
|
{
|
|
|
|
if ( lv && lv->req_elements > 0 ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
lv->selected = MIN ( selected, lv->req_elements - 1 );
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->barview.direction = LEFT_TO_RIGHT;
|
2016-10-08 16:57:59 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void listview_resize ( widget *wid, short w, short h )
|
|
|
|
{
|
|
|
|
listview *lv = (listview *) wid;
|
2017-01-08 20:36:06 +00:00
|
|
|
lv->widget.w = MAX ( 0, w );
|
|
|
|
lv->widget.h = MAX ( 0, h );
|
|
|
|
int height = lv->widget.h - widget_padding_get_padding_height ( WIDGET ( lv ) );
|
2017-06-02 14:34:52 +00:00
|
|
|
int spacing_vert = distance_get_pixel ( lv->spacing, ROFI_ORIENTATION_VERTICAL );
|
2017-01-03 18:23:09 +00:00
|
|
|
lv->max_rows = ( spacing_vert + height ) / ( lv->element_height + spacing_vert );
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->max_elements = lv->max_rows * lv->menu_columns;
|
|
|
|
|
2020-04-26 09:43:14 +00:00
|
|
|
widget_move ( WIDGET ( lv->scrollbar ),
|
2020-08-28 14:41:59 +00:00
|
|
|
lv->widget.w - widget_padding_get_right ( WIDGET ( lv ) ) - widget_get_width ( WIDGET ( lv->scrollbar ) ),
|
|
|
|
widget_padding_get_top ( WIDGET ( lv ) ) );
|
2020-04-26 09:43:14 +00:00
|
|
|
|
2016-12-28 12:17:02 +00:00
|
|
|
widget_resize ( WIDGET ( lv->scrollbar ), widget_get_width ( WIDGET ( lv->scrollbar ) ), height );
|
2016-10-08 16:57:59 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
lv->max_elements = lv->menu_lines;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
listview_recompute_elements ( lv );
|
|
|
|
widget_queue_redraw ( wid );
|
|
|
|
}
|
|
|
|
|
2017-05-30 10:37:11 +00:00
|
|
|
static widget *listview_find_mouse_target ( widget *wid, WidgetType type, gint x, gint y )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2017-05-28 00:18:09 +00:00
|
|
|
widget *target = NULL;
|
|
|
|
gint rx, ry;
|
2016-10-08 16:57:59 +00:00
|
|
|
listview *lv = (listview *) wid;
|
2017-05-30 10:37:11 +00:00
|
|
|
if ( widget_enabled ( WIDGET ( lv->scrollbar ) ) && widget_intersect ( WIDGET ( lv->scrollbar ), x, y ) ) {
|
|
|
|
rx = x - widget_get_x_pos ( WIDGET ( lv->scrollbar ) );
|
|
|
|
ry = y - widget_get_y_pos ( WIDGET ( lv->scrollbar ) );
|
|
|
|
target = widget_find_mouse_target ( WIDGET ( lv->scrollbar ), type, rx, ry );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int max = MIN ( lv->cur_elements, lv->req_elements - lv->last_offset );
|
2017-05-28 00:18:09 +00:00
|
|
|
unsigned int i;
|
|
|
|
for ( i = 0; i < max && target == NULL; i++ ) {
|
2019-08-08 18:02:20 +00:00
|
|
|
widget *w = WIDGET ( lv->boxes[i].box );
|
2017-05-30 10:37:11 +00:00
|
|
|
if ( widget_intersect ( w, x, y ) ) {
|
|
|
|
rx = x - widget_get_x_pos ( w );
|
|
|
|
ry = y - widget_get_y_pos ( w );
|
|
|
|
target = widget_find_mouse_target ( w, type, rx, ry );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 10:37:11 +00:00
|
|
|
return target;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 10:45:48 +00:00
|
|
|
static WidgetTriggerActionResult listview_trigger_action ( widget *wid, MouseBindingListviewAction action, G_GNUC_UNUSED gint x, G_GNUC_UNUSED gint y, G_GNUC_UNUSED void *user_data )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
listview *lv = (listview *) wid;
|
2017-05-28 00:18:09 +00:00
|
|
|
switch ( action )
|
|
|
|
{
|
|
|
|
case SCROLL_LEFT:
|
|
|
|
listview_nav_left ( lv );
|
|
|
|
break;
|
|
|
|
case SCROLL_RIGHT:
|
|
|
|
listview_nav_right ( lv );
|
|
|
|
break;
|
|
|
|
case SCROLL_DOWN:
|
|
|
|
listview_nav_down ( lv );
|
|
|
|
break;
|
|
|
|
case SCROLL_UP:
|
|
|
|
listview_nav_up ( lv );
|
|
|
|
break;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2017-05-30 10:45:48 +00:00
|
|
|
return WIDGET_TRIGGER_ACTION_RESULT_HANDLED;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 10:45:48 +00:00
|
|
|
static WidgetTriggerActionResult listview_element_trigger_action ( widget *wid, MouseBindingListviewElementAction action, G_GNUC_UNUSED gint x, G_GNUC_UNUSED gint y, void *user_data )
|
2016-10-25 19:19:39 +00:00
|
|
|
{
|
2017-05-28 00:18:09 +00:00
|
|
|
listview *lv = (listview *) user_data;
|
|
|
|
unsigned int max = MIN ( lv->cur_elements, lv->req_elements - lv->last_offset );
|
|
|
|
unsigned int i;
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( i = 0; i < max && WIDGET ( lv->boxes[i].box ) != wid; i++ ) {
|
2017-05-28 00:18:09 +00:00
|
|
|
}
|
|
|
|
if ( i == max ) {
|
2017-05-30 10:45:48 +00:00
|
|
|
return WIDGET_TRIGGER_ACTION_RESULT_IGNORED;
|
2016-10-25 19:19:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 00:18:09 +00:00
|
|
|
gboolean custom = FALSE;
|
|
|
|
switch ( action )
|
|
|
|
{
|
|
|
|
case SELECT_HOVERED_ENTRY:
|
|
|
|
listview_set_selected ( lv, lv->last_offset + i );
|
|
|
|
break;
|
|
|
|
case ACCEPT_HOVERED_CUSTOM:
|
|
|
|
custom = TRUE;
|
2017-10-05 15:45:50 +00:00
|
|
|
/* FALLTHRU */
|
2017-05-28 00:18:09 +00:00
|
|
|
case ACCEPT_HOVERED_ENTRY:
|
|
|
|
listview_set_selected ( lv, lv->last_offset + i );
|
|
|
|
lv->mouse_activated ( lv, custom, lv->mouse_activated_data );
|
|
|
|
break;
|
|
|
|
}
|
2017-05-30 10:45:48 +00:00
|
|
|
return WIDGET_TRIGGER_ACTION_RESULT_HANDLED;
|
2016-10-25 19:19:39 +00:00
|
|
|
}
|
2017-05-28 00:18:09 +00:00
|
|
|
|
2017-09-07 11:46:09 +00:00
|
|
|
listview *listview_create ( widget *parent, const char *name, listview_update_callback cb, void *udata, unsigned int eh, gboolean reverse )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2017-10-05 15:45:50 +00:00
|
|
|
listview *lv = g_malloc0 ( sizeof ( listview ) );
|
|
|
|
widget_init ( WIDGET ( lv ), parent, WIDGET_TYPE_LISTVIEW, name );
|
2017-01-08 15:09:24 +00:00
|
|
|
lv->listview_name = g_strdup ( name );
|
2016-12-30 17:31:30 +00:00
|
|
|
lv->widget.free = listview_free;
|
|
|
|
lv->widget.resize = listview_resize;
|
2017-05-23 07:45:42 +00:00
|
|
|
lv->widget.draw = _listview_draw;
|
2017-05-28 00:18:09 +00:00
|
|
|
lv->widget.find_mouse_target = listview_find_mouse_target;
|
|
|
|
lv->widget.trigger_action = listview_trigger_action;
|
2016-12-30 17:31:30 +00:00
|
|
|
lv->widget.get_desired_height = listview_get_desired_height;
|
2017-01-08 20:36:06 +00:00
|
|
|
lv->eh = eh;
|
2016-10-08 16:57:59 +00:00
|
|
|
|
2019-05-04 09:01:09 +00:00
|
|
|
lv->emode = PANGO_ELLIPSIZE_END;
|
2017-10-05 15:45:50 +00:00
|
|
|
lv->scrollbar = scrollbar_create ( WIDGET ( lv ), "scrollbar" );
|
2016-10-08 16:57:59 +00:00
|
|
|
// Calculate height of an element.
|
2016-12-28 12:46:53 +00:00
|
|
|
//
|
2019-08-08 18:02:20 +00:00
|
|
|
_listview_row row;
|
2020-02-02 12:56:37 +00:00
|
|
|
listview_create_row ( lv, &row );
|
2020-03-29 14:31:47 +00:00
|
|
|
// FIXME: hack to scale hight correctly.
|
|
|
|
if ( lv->eh > 1 && row.textbox ) {
|
2020-08-28 14:41:59 +00:00
|
|
|
char buff[lv->eh * 2 + 1];
|
|
|
|
memset ( buff, '\0', lv->eh * 2 + 1 );
|
|
|
|
for ( unsigned int i = 0; i < ( lv->eh - 1 ); i++ ) {
|
|
|
|
buff[i * 2] = 'a'; buff[i * 2 + 1] = '\n';
|
|
|
|
}
|
|
|
|
;
|
|
|
|
textbox_text ( row.textbox, buff );
|
2020-03-29 14:31:47 +00:00
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
lv->element_height = widget_get_desired_height ( WIDGET ( row.box ) );
|
2019-08-08 18:02:20 +00:00
|
|
|
widget_free ( WIDGET ( row.box ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
|
|
|
|
lv->callback = cb;
|
|
|
|
lv->udata = udata;
|
2016-12-11 16:06:31 +00:00
|
|
|
|
|
|
|
// Some settings.
|
2017-01-04 21:27:27 +00:00
|
|
|
lv->spacing = rofi_theme_get_distance ( WIDGET ( lv ), "spacing", DEFAULT_SPACING );
|
|
|
|
lv->menu_columns = rofi_theme_get_integer ( WIDGET ( lv ), "columns", config.menu_columns );
|
|
|
|
lv->fixed_num_lines = rofi_theme_get_boolean ( WIDGET ( lv ), "fixed-height", config.fixed_num_lines );
|
2017-01-08 20:36:06 +00:00
|
|
|
lv->dynamic = rofi_theme_get_boolean ( WIDGET ( lv ), "dynamic", TRUE );
|
|
|
|
lv->reverse = rofi_theme_get_boolean ( WIDGET ( lv ), "reverse", reverse );
|
2017-06-03 18:35:50 +00:00
|
|
|
lv->cycle = rofi_theme_get_boolean ( WIDGET ( lv ), "cycle", config.cycle );
|
2019-08-08 18:02:20 +00:00
|
|
|
lv->fixed_columns = rofi_theme_get_boolean ( WIDGET ( lv ), "fixed-columns", FALSE );
|
2016-12-11 16:06:31 +00:00
|
|
|
|
2017-06-03 19:46:21 +00:00
|
|
|
lv->type = rofi_theme_get_orientation ( WIDGET ( lv ), "layout", ROFI_ORIENTATION_VERTICAL );
|
2017-05-23 08:48:02 +00:00
|
|
|
if ( lv->type == LISTVIEW ) {
|
|
|
|
listview_set_show_scrollbar ( lv, rofi_theme_get_boolean ( WIDGET ( lv ), "scrollbar", FALSE ) );
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-23 08:48:02 +00:00
|
|
|
listview_set_show_scrollbar ( lv, FALSE );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
return lv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigation commands.
|
|
|
|
*/
|
|
|
|
|
2017-01-03 19:22:29 +00:00
|
|
|
static void listview_nav_up_int ( listview *lv )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( lv->req_elements == 0 || ( lv->selected == 0 && !lv->cycle ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( lv->selected == 0 ) {
|
|
|
|
lv->selected = lv->req_elements;
|
|
|
|
}
|
|
|
|
lv->selected--;
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->barview.direction = RIGHT_TO_LEFT;
|
2016-10-08 16:57:59 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
2017-01-03 19:22:29 +00:00
|
|
|
static void listview_nav_down_int ( listview *lv )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( lv->req_elements == 0 || ( lv->selected == ( lv->req_elements - 1 ) && !lv->cycle ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
lv->selected = lv->selected < lv->req_elements - 1 ? MIN ( lv->req_elements - 1, lv->selected + 1 ) : 0;
|
2017-05-23 08:48:02 +00:00
|
|
|
lv->barview.direction = LEFT_TO_RIGHT;
|
2016-10-08 16:57:59 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:22:29 +00:00
|
|
|
void listview_nav_up ( listview *lv )
|
|
|
|
{
|
2017-04-02 10:32:11 +00:00
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-03 19:22:29 +00:00
|
|
|
if ( lv->reverse ) {
|
|
|
|
listview_nav_down_int ( lv );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_up_int ( lv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void listview_nav_down ( listview *lv )
|
|
|
|
{
|
2017-04-02 10:32:11 +00:00
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-03 19:22:29 +00:00
|
|
|
if ( lv->reverse ) {
|
|
|
|
listview_nav_up_int ( lv );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_down_int ( lv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
void listview_nav_left ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
|
|
|
listview_nav_up_int ( lv );
|
2017-05-23 07:45:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( lv->selected >= lv->max_rows ) {
|
|
|
|
lv->selected -= lv->max_rows;
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void listview_nav_right ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-10 20:26:37 +00:00
|
|
|
if ( lv->max_rows == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
|
|
|
listview_nav_down_int ( lv );
|
2017-05-23 07:45:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( ( lv->selected + lv->max_rows ) < lv->req_elements ) {
|
|
|
|
lv->selected += lv->max_rows;
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
else if ( lv->selected < ( lv->req_elements - 1 ) ) {
|
|
|
|
// We do not want to move to last item, UNLESS the last column is only
|
|
|
|
// partially filled, then we still want to move column and select last entry.
|
|
|
|
// First check the column we are currently in.
|
|
|
|
int col = lv->selected / lv->max_rows;
|
|
|
|
// Check total number of columns.
|
|
|
|
int ncol = lv->req_elements / lv->max_rows;
|
|
|
|
// If there is an extra column, move.
|
|
|
|
if ( col != ncol ) {
|
|
|
|
lv->selected = lv->req_elements - 1;
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:22:29 +00:00
|
|
|
static void listview_nav_page_prev_int ( listview *lv )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
|
|
|
if ( lv->last_offset == 0 ) {
|
2017-05-24 06:10:03 +00:00
|
|
|
lv->selected = 0;
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv->selected = lv->last_offset - 1;
|
2017-05-24 06:10:03 +00:00
|
|
|
}
|
|
|
|
lv->barview.direction = RIGHT_TO_LEFT;
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( lv->selected < lv->max_elements ) {
|
|
|
|
lv->selected = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
lv->selected -= ( lv->max_elements );
|
|
|
|
}
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
2017-01-03 19:22:29 +00:00
|
|
|
static void listview_nav_page_next_int ( listview *lv )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( lv->req_elements == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-24 06:10:03 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
unsigned int new = lv->last_offset + lv->barview.cur_visible;
|
|
|
|
lv->selected = MIN ( new, lv->req_elements - 1 );
|
2017-05-24 06:10:03 +00:00
|
|
|
lv->barview.direction = LEFT_TO_RIGHT;
|
|
|
|
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
return;
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
lv->selected += ( lv->max_elements );
|
|
|
|
if ( lv->selected >= lv->req_elements ) {
|
|
|
|
lv->selected = lv->req_elements - 1;
|
|
|
|
}
|
|
|
|
widget_queue_redraw ( WIDGET ( lv ) );
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:22:29 +00:00
|
|
|
void listview_nav_page_prev ( listview *lv )
|
|
|
|
{
|
2017-04-02 10:32:11 +00:00
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( lv->reverse ) {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_page_next_int ( lv );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_page_prev_int ( lv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void listview_nav_page_next ( listview *lv )
|
|
|
|
{
|
2017-04-02 10:32:11 +00:00
|
|
|
if ( lv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( lv->reverse ) {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_page_prev_int ( lv );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-03 19:22:29 +00:00
|
|
|
listview_nav_page_next_int ( lv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:00:06 +00:00
|
|
|
static int listview_get_desired_height ( widget *wid )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2017-01-09 21:40:11 +00:00
|
|
|
listview *lv = (listview *) wid;
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( lv == NULL || lv->widget.enabled == FALSE ) {
|
2016-10-08 16:57:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-02 14:34:52 +00:00
|
|
|
int spacing = distance_get_pixel ( lv->spacing, ROFI_ORIENTATION_VERTICAL );
|
2017-01-09 21:40:11 +00:00
|
|
|
int h = lv->menu_lines;
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( !( lv->fixed_num_lines ) ) {
|
2017-01-03 13:25:24 +00:00
|
|
|
if ( lv->dynamic ) {
|
|
|
|
h = MIN ( lv->menu_lines, lv->req_elements );
|
2017-01-08 20:36:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-03 13:25:24 +00:00
|
|
|
h = MIN ( lv->menu_lines, lv->max_displayed_lines );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( lv->type == BARVIEW ) {
|
2017-05-23 07:45:42 +00:00
|
|
|
h = MIN ( h, 1 );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( h == 0 ) {
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( lv->dynamic && !lv->fixed_num_lines ) {
|
2017-01-06 12:42:00 +00:00
|
|
|
// Hide widget fully.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
return widget_padding_get_padding_height ( WIDGET ( lv ) );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
int height = widget_padding_get_padding_height ( WIDGET ( lv ) );
|
|
|
|
height += h * ( lv->element_height + spacing ) - spacing;
|
2017-01-02 23:59:37 +00:00
|
|
|
return height;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 21:16:31 +00:00
|
|
|
void listview_set_show_scrollbar ( listview *lv, gboolean enabled )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
if ( enabled ) {
|
|
|
|
widget_enable ( WIDGET ( lv->scrollbar ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget_disable ( WIDGET ( lv->scrollbar ) );
|
|
|
|
}
|
|
|
|
listview_recompute_elements ( lv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void listview_set_scroll_type ( listview *lv, ScrollType type )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->scroll_type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void listview_set_mouse_activated_cb ( listview *lv, listview_mouse_activated_cb cb, void *udata )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->mouse_activated = cb;
|
|
|
|
lv->mouse_activated_data = udata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void listview_set_multi_select ( listview *lv, gboolean enable )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->multi_select = enable;
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 20:19:27 +00:00
|
|
|
void listview_set_num_lines ( listview *lv, unsigned int num_lines )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->menu_lines = num_lines;
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 15:49:46 +00:00
|
|
|
|
|
|
|
unsigned int listview_get_num_lines ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
return lv->menu_lines;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-02 20:19:27 +00:00
|
|
|
void listview_set_max_lines ( listview *lv, unsigned int max_lines )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->max_displayed_lines = max_lines;
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 16:18:49 +00:00
|
|
|
|
|
|
|
gboolean listview_get_fixed_num_lines ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
return lv->fixed_num_lines;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
void listview_set_fixed_num_lines ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->fixed_num_lines = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 09:01:09 +00:00
|
|
|
|
2020-04-16 21:31:43 +00:00
|
|
|
void listview_set_ellipsize_start ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
|
|
|
lv->emode = PANGO_ELLIPSIZE_START;
|
|
|
|
for ( unsigned int i = 0; i < lv->cur_elements; i++ ) {
|
|
|
|
textbox_set_ellipsize ( lv->boxes[i].textbox, lv->emode );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 09:01:09 +00:00
|
|
|
void listview_toggle_ellipsizing ( listview *lv )
|
|
|
|
{
|
|
|
|
if ( lv ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
PangoEllipsizeMode mode = lv->emode;
|
2019-12-29 16:30:53 +00:00
|
|
|
if ( mode == PANGO_ELLIPSIZE_START ) {
|
2019-05-04 09:01:09 +00:00
|
|
|
mode = PANGO_ELLIPSIZE_MIDDLE;
|
2020-02-02 12:56:37 +00:00
|
|
|
}
|
|
|
|
else if ( mode == PANGO_ELLIPSIZE_MIDDLE ) {
|
2019-05-04 09:01:09 +00:00
|
|
|
mode = PANGO_ELLIPSIZE_END;
|
2020-02-02 12:56:37 +00:00
|
|
|
}
|
|
|
|
else if ( mode == PANGO_ELLIPSIZE_END ) {
|
2019-05-04 09:01:09 +00:00
|
|
|
mode = PANGO_ELLIPSIZE_START;
|
|
|
|
}
|
|
|
|
lv->emode = mode;
|
|
|
|
for ( unsigned int i = 0; i < lv->cur_elements; i++ ) {
|
2019-08-08 18:02:20 +00:00
|
|
|
textbox_set_ellipsize ( lv->boxes[i].textbox, mode );
|
2019-05-04 09:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|