diff --git a/src/list_window.c b/src/list_window.c
index f7da79ea147973f67db1b44acc86e791bd3076b3..ca857b8a06ec60185e3d9d334c318d084b881b55 100644 (file)
--- a/src/list_window.c
+++ b/src/list_window.c
/* ncmpc (Ncurses MPD Client)
* (c) 2004-2009 The Music Player Daemon Project
* Project homepage: http://musicpd.org
-
+
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
#include "match.h"
#include "command.h"
#include "colors.h"
+#include "screen_message.h"
+#include "i18n.h"
#include <assert.h>
#include <stdlib.h>
lw->w = w;
lw->cols = width;
lw->rows = height;
+ lw->range_selection = false;
return lw;
}
list_window_reset(struct list_window *lw)
{
lw->selected = 0;
- lw->xoffset = 0;
+ lw->selected_start = 0;
+ lw->selected_end = 0;
+ lw->range_selection = false;
+ lw->range_base = 0;
lw->start = 0;
}
lw->selected = 0;
else if (lw->selected >= length)
lw->selected = length - 1;
+
+ if(lw->range_selection)
+ {
+ if (length == 0) {
+ lw->selected_start = 0;
+ lw->selected_end = 0;
+ lw->range_base = 0;
+ } else {
+ if (lw->selected_start >= length)
+ lw->selected_start = length - 1;
+ if (lw->selected_end >= length)
+ lw->selected_end = length - 1;
+ if (lw->range_base >= length)
+ lw->range_base = length - 1;
+ }
+
+ if(lw->range_base > lw->selected_end)
+ lw->selected_end = lw->selected;
+ if(lw->range_base < lw->selected_start)
+ lw->selected_start = lw->selected;
+ }
+ else
+ {
+ lw->selected_start = lw->selected;
+ lw->selected_end = lw->selected;
+ }
}
void
list_window_set_selected(struct list_window *lw, unsigned n)
{
lw->selected = n;
+ if(lw->range_selection)
+ {
+ if(n >= lw->range_base)
+ {
+ lw->selected_end = n;
+ lw->selected_start = lw->range_base;
+ }
+ if(n <= lw->range_base)
+ {
+ lw->selected_start = n;
+ lw->selected_end = lw->range_base;
+ }
+ }
+ else
+ {
+ lw->selected_start = n;
+ lw->selected_end = n;
+ }
}
static void
list_window_next(struct list_window *lw, unsigned length)
{
if (lw->selected + 1 < length)
- lw->selected++;
+ list_window_set_selected(lw, lw->selected + 1);
else if (options.list_wrap)
- lw->selected = 0;
+ list_window_set_selected(lw, 0);
}
static void
list_window_previous(struct list_window *lw, unsigned length)
{
if (lw->selected > 0)
- lw->selected--;
+ list_window_set_selected(lw, lw->selected - 1);
else if (options.list_wrap)
- lw->selected = length - 1;
+ list_window_set_selected(lw, length-1);
+}
+
+static void
+list_window_top(struct list_window *lw)
+{
+ if (lw->start == 0)
+ list_window_set_selected(lw, lw->start);
+ else
+ if ((unsigned) options.scroll_offset * 2 >= lw->rows)
+ list_window_set_selected(lw, lw->start + lw->rows / 2);
+ else
+ list_window_set_selected(lw, lw->start + options.scroll_offset);
+}
+
+static void
+list_window_middle(struct list_window *lw, unsigned length)
+{
+ if (length >= lw->rows)
+ list_window_set_selected(lw, lw->start + lw->rows / 2);
+ else
+ list_window_set_selected(lw, length / 2);
+}
+
+static void
+list_window_bottom(struct list_window *lw, unsigned length)
+{
+ if (length >= lw->rows)
+ if ((unsigned) options.scroll_offset * 2 >= lw->rows)
+ list_window_set_selected(lw, lw->start + lw->rows / 2);
+ else
+ if (lw->start + lw->rows == length)
+ list_window_set_selected(lw, length - 1);
+ else
+ list_window_set_selected(lw, lw->start + lw->rows - 1 - options.scroll_offset);
+ else
+ list_window_set_selected(lw, length - 1);
}
static void
list_window_first(struct list_window *lw)
{
- lw->xoffset = 0;
- lw->selected = 0;
+ list_window_set_selected(lw, 0);
}
static void
list_window_last(struct list_window *lw, unsigned length)
{
- lw->xoffset = 0;
if (length > 0)
- lw->selected = length - 1;
+ list_window_set_selected(lw, length - 1);
else
- lw->selected = 0;
+ list_window_set_selected(lw, 0);
}
static void
if (lw->rows < 2)
return;
if (lw->selected + lw->rows < length)
- lw->selected += lw->rows - 1;
+ list_window_set_selected(lw, lw->selected + lw->rows - 1);
else
list_window_last(lw, length);
}
if (lw->rows < 2)
return;
if (lw->selected > lw->rows - 1)
- lw->selected -= lw->rows - 1;
+ list_window_set_selected(lw, lw->selected - lw->rows + 1);
else
list_window_first(lw);
}
+static void
+list_window_scroll_up(struct list_window *lw, unsigned n)
+{
+ if (lw->start > 0) {
+ if (n > lw->start)
+ lw->start = 0;
+ else
+ lw->start -= n;
+ if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
+ lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
+ if (lw->range_selection) {
+ if (lw->selected < lw->range_base) {
+ lw->selected_start = lw->selected;
+ lw->selected_end = lw->range_base;
+ } else {
+ lw->selected_end = lw->selected;
+ }
+ } else {
+ lw->selected_start = lw->selected;
+ lw->selected_end = lw->selected;
+ }
+ }
+ }
+}
+
+static void
+list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
+{
+ if (lw->start + lw->rows < length)
+ {
+ if ( lw->start + lw->rows + n > length - 1)
+ lw->start = length - lw->rows;
+ else
+ lw->start += n;
+ if (lw->selected < lw->start + options.scroll_offset) {
+ lw->selected = lw->start + options.scroll_offset;
+ if (lw->range_selection) {
+ if (lw->selected > lw->range_base) {
+ lw->selected_end = lw->selected;
+ lw->selected_start = lw->range_base;
+ } else {
+ lw->selected_start = lw->selected;
+ }
+ } else {
+ lw->selected_start = lw->selected;
+ lw->selected_end = lw->selected;
+ }
+ }
+ }
+}
void
list_window_paint(struct list_window *lw,
unsigned i;
bool fill = options.wide_cursor;
bool show_cursor = !lw->hide_cursor;
+ bool highlight = false;
if (show_cursor) {
- if (lw->selected < lw->start)
- lw->start = lw->selected;
+ int start = lw->start;
+ if ((unsigned) options.scroll_offset * 2 >= lw->rows)
+ // Center if the offset is more than half the screen
+ start = lw->selected - lw->rows / 2;
+ else
+ {
+ if (lw->selected < lw->start + options.scroll_offset)
+ start = lw->selected - options.scroll_offset;
- if (lw->selected >= lw->start + lw->rows)
- lw->start = lw->selected - lw->rows + 1;
+ if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
+ {
+ start = lw->selected - lw->rows + 1 + options.scroll_offset;
+ }
+ }
+ if (start < 0)
+ lw->start = 0;
+ else
+ {
+ while ( start > 0 && callback(start + lw->rows - 1, &highlight, NULL, callback_data) == NULL)
+ start--;
+ lw->start = start;
+ }
}
for (i = 0; i < lw->rows; i++) {
- bool highlight = false;
const char *label;
+ char *second_column = NULL;
+ highlight = false;
- label = callback(lw->start + i, &highlight, callback_data);
+ label = callback(lw->start + i, &highlight, &second_column, callback_data);
wmove(lw->w, i, 0);
if (label) {
- bool selected = lw->start + i == lw->selected;
+ bool selected = (lw->start + i >= lw->selected_start && lw->start + i <= lw->selected_end);
unsigned len = utf8_width(label);
if (highlight)
else
colors_use(lw->w, COLOR_LIST);
- if (show_cursor && selected)
+ if (show_cursor && selected &&
+ (!options.hardware_cursor || lw->range_selection))
wattron(lw->w, A_REVERSE);
//waddnstr(lw->w, label, lw->cols);
if (fill && len < lw->cols)
whline(lw->w, ' ', lw->cols-len);
+ if(second_column)
+ {
+ unsigned sc_len = utf8_width(second_column) + 1;
+ if(lw->cols > sc_len)
+ {
+ wmove(lw->w, i, lw->cols - sc_len);
+ waddstr(lw->w, " ");
+ wmove(lw->w, i, lw->cols - sc_len + 1);
+ waddstr(lw->w, second_column);
+ }
+ g_free(second_column);
+ }
+
if (selected)
wattroff(lw->w, A_REVERSE);
} else
wclrtoeol(lw->w);
}
+
+ if (options.hardware_cursor && lw->selected >= lw->start &&
+ lw->selected < lw->start + lw->rows) {
+ curs_set(1);
+ wmove(lw->w, lw->selected - lw->start, 0);
+ }
}
bool
const char *label;
do {
- while ((label = callback(i,&h,callback_data))) {
+ while ((label = callback(i,&h,NULL,callback_data))) {
if (str && label && match_line(label, str)) {
lw->selected = i;
+ if(!lw->range_selection || i > lw->selected_end)
+ lw->selected_end = i;
+ if(!lw->range_selection || i < lw->selected_start)
+ lw->selected_start = i;
return true;
}
if (wrap && i == lw->selected)
return false;
do {
- while (i >= 0 && (label = callback(i,&h,callback_data))) {
+ while (i >= 0 && (label = callback(i,&h,NULL,callback_data))) {
if( str && label && match_line(label, str) ) {
lw->selected = i;
+ if(!lw->range_selection || i > (int)lw->selected_end)
+ lw->selected_end = i;
+ if(!lw->range_selection || i < (int)lw->selected_start)
+ lw->selected_start = i;
return true;
}
if (wrap && i == (int)lw->selected)
return false;
}
+bool
+list_window_jump(struct list_window *lw,
+ list_window_callback_fn_t callback,
+ void *callback_data,
+ const char *str)
+{
+ bool h;
+ unsigned i = 0;
+ const char *label;
+
+ while ((label = callback(i,&h,NULL,callback_data))) {
+ if (label && label[0] == '[')
+ label++;
+#ifndef NCMPC_MINI
+ if (str && label &&
+ ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
+ (!options.jump_prefix_only && match_line(label, str))) )
+#else
+ if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
+#endif
+ {
+ lw->selected = i;
+ if(!lw->range_selection || i > lw->selected_end)
+ lw->selected_end = i;
+ if(!lw->range_selection || i < lw->selected_start)
+ lw->selected_start = i;
+ return true;
+ }
+ i++;
+ }
+ return false;
+}
+
/* perform basic list window commands (movement) */
bool
list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
case CMD_LIST_NEXT:
list_window_next(lw, rows);
break;
+ case CMD_LIST_TOP:
+ list_window_top(lw);
+ break;
+ case CMD_LIST_MIDDLE:
+ list_window_middle(lw,rows);
+ break;
+ case CMD_LIST_BOTTOM:
+ list_window_bottom(lw,rows);
+ break;
case CMD_LIST_FIRST:
list_window_first(lw);
break;
case CMD_LIST_PREVIOUS_PAGE:
list_window_previous_page(lw);
break;
+ case CMD_LIST_RANGE_SELECT:
+ if(lw->range_selection)
+ {
+ screen_status_printf(_("Range selection disabled"));
+ lw->range_selection = false;
+ list_window_set_selected(lw, lw->selected);
+ }
+ else
+ {
+ screen_status_printf(_("Range selection enabled"));
+ lw->range_base = lw->selected;
+ lw->range_selection = true;
+ }
+ break;
+ case CMD_LIST_SCROLL_UP_LINE:
+ list_window_scroll_up(lw, 1);
+ break;
+ case CMD_LIST_SCROLL_DOWN_LINE:
+ list_window_scroll_down(lw, rows, 1);
+ break;
+ case CMD_LIST_SCROLL_UP_HALF:
+ list_window_scroll_up(lw, (lw->rows - 1) / 2);
+ break;
+ case CMD_LIST_SCROLL_DOWN_HALF:
+ list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
+ break;
default:
return false;
}
list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
{
switch (cmd) {
+ case CMD_LIST_SCROLL_UP_LINE:
case CMD_LIST_PREVIOUS:
if (lw->start > 0)
lw->start--;
break;
+ case CMD_LIST_SCROLL_DOWN_LINE:
case CMD_LIST_NEXT:
if (lw->start + lw->rows < rows)
lw->start++;
lw->start = 0;
break;
+ case CMD_LIST_SCROLL_UP_HALF:
+ if (lw->start > (lw->rows - 1) / 2)
+ lw->start -= (lw->rows - 1) / 2;
+ else
+ lw->start = 0;
+ break;
+
+ case CMD_LIST_SCROLL_DOWN_HALF:
+ lw->start += (lw->rows - 1) / 2;
+ if (lw->start + lw->rows > rows) {
+ if (rows > lw->rows)
+ lw->start = rows - lw->rows;
+ else
+ lw->start = 0;
+ }
+ break;
+
default:
return false;
}
{
assert(lw != NULL);
- /* if the even occured above the list window move up */
+ /* if the even occurred above the list window move up */
if (y < 0) {
if (bstate & BUTTON3_CLICKED)
list_window_first(lw);
return true;
}
- /* if the even occured below the list window move down */
+ /* if the even occurred below the list window move down */
if ((unsigned)y >= rows) {
if (bstate & BUTTON3_CLICKED)
list_window_last(lw, rows);