Code

Rename variable sun as it is predefined (to 1) on solaris
[ncmpc.git] / src / list_window.c
index ad671f0cf51ad1a36715e6e1623731d36cb1acc5..010deb86002916951dbee3572d35112f9d13ee9d 100644 (file)
@@ -1,6 +1,4 @@
-/* 
- * $Id$
- *
+/*
  * (c) 2004 by Kalle Wallin <kaw@linux.se>
  *
  * This program is free software; you can redistribute it and/or modify
 #include "list_window.h"
 #include "config.h"
 #include "options.h"
-#include "support.h"
+#include "charset.h"
+#include "match.h"
 #include "command.h"
 #include "colors.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -40,19 +40,16 @@ list_window_init(WINDOW *w, unsigned width, unsigned height)
        lw->w = w;
        lw->cols = width;
        lw->rows = height;
-       lw->clear = 1;
        return lw;
 }
 
-struct list_window *
+void
 list_window_free(struct list_window *lw)
 {
        if (lw) {
                memset(lw, 0, sizeof(list_window_t));
                g_free(lw);
        }
-
-       return NULL;
 }
 
 void
@@ -61,7 +58,6 @@ list_window_reset(struct list_window *lw)
        lw->selected = 0;
        lw->xoffset = 0;
        lw->start = 0;
-       lw->clear = 1;
 }
 
 void
@@ -77,7 +73,9 @@ list_window_check_selected(struct list_window *lw, unsigned length)
        if (lw->selected < lw->start)
                lw->selected = lw->start;
 
-       if (length > 0 && lw->selected >= length)
+       if (length == 0)
+               lw->selected = 0;
+       else if (lw->selected >= length)
                lw->selected = length - 1;
 }
 
@@ -95,8 +93,6 @@ list_window_center(struct list_window *lw, unsigned rows, unsigned n)
                else
                        lw->start = 0;
        }
-
-       lw->repaint = lw->clear = 1;
 }
 
 void
@@ -105,7 +101,7 @@ list_window_set_selected(struct list_window *lw, unsigned n)
        lw->selected = n;
 }
 
-void
+static void
 list_window_next(struct list_window *lw, unsigned length)
 {
        if (lw->selected + 1 < length)
@@ -114,7 +110,7 @@ list_window_next(struct list_window *lw, unsigned length)
                lw->selected = 0;
 }
 
-void
+static void
 list_window_previous(struct list_window *lw, unsigned length)
 {
        if (lw->selected > 0)
@@ -123,14 +119,14 @@ list_window_previous(struct list_window *lw, unsigned length)
                lw->selected = length - 1;
 }
 
-void
+static void
 list_window_first(struct list_window *lw)
 {
        lw->xoffset = 0;
        lw->selected = 0;
 }
 
-void
+static void
 list_window_last(struct list_window *lw, unsigned length)
 {
        lw->xoffset = 0;
@@ -140,7 +136,7 @@ list_window_last(struct list_window *lw, unsigned length)
                lw->selected = 0;
 }
 
-void
+static void
 list_window_next_page(struct list_window *lw, unsigned length)
 {
        if (lw->rows < 2)
@@ -148,10 +144,10 @@ list_window_next_page(struct list_window *lw, unsigned length)
        if (lw->selected + lw->rows < length)
                lw->selected += lw->rows - 1;
        else
-               return list_window_last(lw, length);
+               list_window_last(lw, length);
 }
 
-void
+static void
 list_window_previous_page(struct list_window *lw)
 {
        if (lw->rows < 2)
@@ -169,74 +165,70 @@ list_window_paint(struct list_window *lw,
                  void *callback_data)
 {
        unsigned i;
-       int fill = options.wide_cursor;
-       int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
+       bool fill = options.wide_cursor;
+       bool show_cursor = !lw->hide_cursor;
 
        if (show_cursor) {
-               if (lw->selected < lw->start) {
+               if (lw->selected < lw->start)
                        lw->start = lw->selected;
-                       lw->clear=1;
-               }
 
-               if (lw->selected >= lw->start + lw->rows) {
+               if (lw->selected >= lw->start + lw->rows)
                        lw->start = lw->selected - lw->rows + 1;
-                       lw->clear=1;
-               }
        }
 
        for (i = 0; i < lw->rows; i++) {
-               int highlight = 0;
+               bool highlight = false;
                const char *label;
 
                label = callback(lw->start + i, &highlight, callback_data);
                wmove(lw->w, i, 0);
-               if( lw->clear && (!fill || !label) )
-                       wclrtoeol(lw->w);
 
                if (label) {
-                       int selected = lw->start + i == lw->selected;
-                       size_t len = my_strlen(label);
+                       bool selected = lw->start + i == lw->selected;
+                       unsigned len = utf8_width(label);
 
-                       if( highlight )
+                       if (highlight)
                                colors_use(lw->w, COLOR_LIST_BOLD);
                        else
                                colors_use(lw->w, COLOR_LIST);
 
-                       if( show_cursor && selected )
+                       if (show_cursor && selected)
                                wattron(lw->w, A_REVERSE);
 
                        //waddnstr(lw->w, label, lw->cols);
                        waddstr(lw->w, label);
-                       if( fill && len<lw->cols )
+                       if (fill && len < lw->cols)
                                whline(lw->w,  ' ', lw->cols-len);
 
-                       if( selected )
+                       if (selected)
                                wattroff(lw->w, A_REVERSE);
-               }
-       }
 
-       lw->clear=0;
+                       if (!fill && len < lw->cols)
+                               wclrtoeol(lw->w);
+               } else
+                       wclrtoeol(lw->w);
+       }
 }
 
-int
+bool
 list_window_find(struct list_window *lw,
                 list_window_callback_fn_t callback,
                 void *callback_data,
                 const char *str,
-                int wrap)
+                bool wrap)
 {
-       int h;
+       bool h;
        unsigned i = lw->selected + 1;
        const char *label;
 
-       while (wrap || i == lw->selected + 1) {
+       do {
                while ((label = callback(i,&h,callback_data))) {
-                       if (str && label && strcasestr(label, str)) {
+                       if (str && label && match_line(label, str)) {
                                lw->selected = i;
-                               return 0;
+                               return true;
                        }
                        if (wrap && i == lw->selected)
-                               return 1;
+                               return false;
                        i++;
                }
                if (wrap) {
@@ -245,46 +237,47 @@ list_window_find(struct list_window *lw,
                        i=0; /* first item */
                        screen_bell();
                }
-       }
+       } while (wrap);
 
-       return 1;
+       return false;
 }
 
-int
+bool
 list_window_rfind(struct list_window *lw,
                  list_window_callback_fn_t callback,
                  void *callback_data,
                  const char *str,
-                 int wrap,
+                 bool wrap,
                  unsigned rows)
 {
-       int h;
+       bool h;
        int i = lw->selected - 1;
        const char *label;
 
        if (rows == 0)
-               return 1;
+               return false;
 
-       while (wrap || i == (int)lw->selected - 1) {
+       do {
                while (i >= 0 && (label = callback(i,&h,callback_data))) {
-                       if( str && label && strcasestr(label, str) ) {
+                       if( str && label && match_line(label, str) ) {
                                lw->selected = i;
-                               return 0;
+                               return true;
                        }
                        if (wrap && i == (int)lw->selected)
-                               return 1;
+                               return false;
                        i--;
                }
                if (wrap) {
                        i = rows - 1; /* last item */
                        screen_bell();
                }
-       }
-       return 1;
+       } while (wrap);
+
+       return false;
 }
 
 /* perform basic list window commands (movement) */
-int
+bool
 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 {
        switch (cmd) {
@@ -307,14 +300,13 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
                list_window_previous_page(lw);
                break;
        default:
-               return 0;
+               return false;
        }
 
-       lw->repaint  = 1;
-       return 1;
+       return true;
 }
 
-int
+bool
 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 {
        switch (cmd) {
@@ -357,65 +349,37 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
                break;
 
        default:
-               return 0;
+               return false;
        }
 
-       lw->repaint = lw->clear = 1;
-       return 1;
-}
-
-list_window_state_t *
-list_window_init_state(void)
-{
-       return g_malloc0(sizeof(list_window_state_t));
+       return true;
 }
 
-list_window_state_t *
-list_window_free_state(list_window_state_t *state)
+#ifdef HAVE_GETMOUSE
+bool
+list_window_mouse(struct list_window *lw, unsigned rows,
+                 unsigned long bstate, int y)
 {
-       if (state) {
-               if (state->list) {
-                       GList *list = state->list;
-
-                       while (list) {
-                               g_free(list->data);
-                               list->data = NULL;
-                               list = list->next;
-                       }
+       assert(lw != NULL);
 
-                       g_list_free(state->list);
-                       state->list = NULL;
-               }
-
-               g_free(state);
-       }
-
-       return NULL;
-}
-
-void
-list_window_push_state(list_window_state_t *state, struct list_window *lw)
-{
-       if (state) {
-               struct list_window *tmp = g_malloc(sizeof(list_window_t));
-               memcpy(tmp, lw, sizeof(list_window_t));
-               state->list = g_list_prepend(state->list, (gpointer) tmp);
-               list_window_reset(lw);
+       /* if the even occured above the list window move up */
+       if (y < 0) {
+               if (bstate & BUTTON3_CLICKED)
+                       list_window_first(lw);
+               else
+                       list_window_previous_page(lw);
+               return true;
        }
-}
 
-bool
-list_window_pop_state(list_window_state_t *state, struct list_window *lw)
-{
-       if (state && state->list) {
-               struct list_window *tmp = state->list->data;
-
-               memcpy(lw, tmp, sizeof(list_window_t));
-               g_free(tmp);
-               state->list->data = NULL;
-               state->list = g_list_delete_link(state->list, state->list);
+       /* if the even occured below the list window move down */
+       if ((unsigned)y >= rows) {
+               if (bstate & BUTTON3_CLICKED)
+                       list_window_last(lw, rows);
+               else
+                       list_window_next_page(lw, rows);
+               return true;
        }
 
-       // return TRUE if there are still states in the list
-       return (state && state->list) ? TRUE : FALSE;
+       return false;
 }
+#endif