Code

Moved list window fuctions to list_window.c.
authorKalle Wallin <kaw@linux.se>
Wed, 24 Mar 2004 15:17:52 +0000 (15:17 +0000)
committerKalle Wallin <kaw@linux.se>
Wed, 24 Mar 2004 15:17:52 +0000 (15:17 +0000)
Added function list_window_cmd() for basic commands (movment).

git-svn-id: https://svn.musicpd.org/ncmpc/trunk@454 09075e82-0dd4-0310-85a5-a0d7c8717e4f

Makefile.am
list_window.c [new file with mode: 0644]
list_window.h [new file with mode: 0644]
main.c
screen.h
screen_file.c
screen_help.c
screen_play.c
screen_utils.c
screen_utils.h

index 9731afbec33046f948f601ac2f1dd0d40d9c8f29..96e6b1dd05dc72bd28cb9daf94884b1658f644d2 100644 (file)
@@ -11,12 +11,12 @@ EXTRA_DIST = COPYING $(pkgdata_DATA) $(man_MANS) $(doc_DATA)
 
 ncmpc_headers = libmpdclient.h mpc.h options.h command.h screen.h \
               screen_utils.h screen_play.h screen_file.h screen_search.h \
-             screen_help.h support.h
+             screen_help.h list_window.h support.h
 
 ncmpc_SOURCES = libmpdclient.c main.c mpc.c options.c command.c \
                      screen.c screen_utils.c screen_play.c screen_file.c \
                      screen_search.c screen_help.c \
-                      support.c $(ncmpc_headers)
+                      list_window.c support.c $(ncmpc_headers)
 
 
 
diff --git a/list_window.c b/list_window.c
new file mode 100644 (file)
index 0000000..21ee911
--- /dev/null
@@ -0,0 +1,206 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <glib.h>
+#include <ncurses.h>
+
+#include "support.h"
+#include "command.h"
+#include "list_window.h"
+
+list_window_t *
+list_window_init(WINDOW *w, int width, int height)
+{
+  list_window_t *lw;
+
+  lw = malloc(sizeof(list_window_t));
+  memset(lw, 0, sizeof(list_window_t));
+  lw->w = w;
+  lw->cols = width;
+  lw->rows = height;
+  lw->clear = 1;
+  return lw;
+}
+
+list_window_t *
+list_window_free(list_window_t *lw)
+{
+  if( lw )
+    {
+      memset(lw, 0, sizeof(list_window_t));
+      free(lw);
+    }
+  return NULL;
+}
+
+void
+list_window_reset(list_window_t *lw)
+{
+  lw->selected = 0;
+  lw->start = 0;
+  lw->clear = 1;
+}
+
+void 
+list_window_set_selected(list_window_t *lw, int n)
+{
+  lw->selected=n;
+}
+
+void
+list_window_next(list_window_t *lw, int length)
+{
+  if( lw->selected < length-1 )
+    lw->selected++;
+}
+
+void
+list_window_previous(list_window_t *lw)
+{
+  if( lw->selected > 0 )
+    lw->selected--;
+}
+
+void
+list_window_first(list_window_t *lw)
+{
+  lw->selected = 0;
+}
+
+void
+list_window_last(list_window_t *lw, int length)
+{
+  lw->selected = length-1;
+}
+
+void
+list_window_next_page(list_window_t *lw, int length)
+{
+  int step = lw->rows-1;
+  if( step<= 0 )
+    return;
+  if( lw->selected+step < length-1 )
+    lw->selected+=step;
+  else
+    return list_window_last(lw,length);
+}
+
+void
+list_window_previous_page(list_window_t *lw)
+{
+  int step = lw->rows-1;
+  if( step<= 0 )
+    return;
+  if( lw->selected-step > 0 )
+    lw->selected-=step;
+  else
+    list_window_first(lw);
+}
+
+
+void 
+list_window_paint(list_window_t *lw,
+                 list_window_callback_fn_t callback,
+                 void *callback_data)
+{
+  int i;
+
+  while( lw->selected < lw->start )
+    {
+      lw->start--;
+      lw->clear=1;
+    }
+  while( lw->selected >= lw->start+lw->rows )
+    {
+      lw->start++;
+      lw->clear=1;
+    }
+  if( lw->clear )
+    {
+      wclear(lw->w);
+      lw->clear=0;
+    }
+
+  for(i=0; i<lw->rows; i++)
+    {
+      int highlight;
+      char *label;
+
+      label = (callback) (lw->start+i, &highlight, callback_data);
+      if( label )
+       {
+         wmove(lw->w, i, 0);
+         if( highlight )
+           wattron(lw->w, A_BOLD);
+         if( lw->start+i == lw->selected )
+           wattron(lw->w, A_REVERSE);
+         
+         waddnstr(lw->w, label, lw->cols);
+
+         if( highlight )
+           wattroff(lw->w, A_BOLD);
+         if( lw->start+i == lw->selected )
+           wattroff(lw->w, A_REVERSE);
+       }
+    }
+}
+
+
+int
+list_window_find(list_window_t *lw, 
+                list_window_callback_fn_t callback,
+                void *callback_data,
+                char *str)
+{
+  int h;
+  int i = lw->selected+1;
+  char *label;
+
+  while( (label=(callback) (i,&h,callback_data)) )
+    {
+      if( str && label && strcasestr(label, str) )
+       {
+         lw->selected = i;
+         return 0;
+       }
+      i++;
+    }
+  return 1;
+}
+
+
+/* perform basic list window commands (movement) */
+int 
+list_window_cmd(list_window_t *lw, int rows, command_t cmd)
+{
+  switch(cmd)
+    {
+    case CMD_LIST_PREVIOUS:
+      list_window_previous(lw);
+      lw->repaint=1;
+      break;
+    case CMD_LIST_NEXT:
+      list_window_next(lw, rows);
+      lw->repaint=1;
+      break;
+    case CMD_LIST_FIRST:
+      list_window_first(lw);
+      lw->repaint  = 1;
+      break;
+    case CMD_LIST_LAST:
+      list_window_last(lw, rows);
+      lw->repaint = 1;
+      break;
+    case CMD_LIST_NEXT_PAGE:
+      list_window_next_page(lw, rows);
+      lw->repaint  = 1;
+      break;
+    case CMD_LIST_PREVIOUS_PAGE:
+      list_window_previous_page(lw);
+      lw->repaint  = 1;
+      break;
+    default:
+      return 0;
+    }
+  return 1;
+}
diff --git a/list_window.h b/list_window.h
new file mode 100644 (file)
index 0000000..87bbf82
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef LIST_WINDOW_H
+#define LIST_WINDOW_H
+
+typedef char *  (*list_window_callback_fn_t)   (int index, 
+                                               int *highlight,
+                                               void *data);
+
+typedef struct
+{
+  WINDOW *w;
+  int rows, cols;
+
+  int start;
+  int selected;
+  int clear;
+  int repaint;
+
+} list_window_t;
+
+
+/* create a new list window */
+list_window_t *list_window_init(WINDOW *w, int width, int height);
+
+/* destroy a list window (returns NULL) */
+list_window_t *list_window_free(list_window_t *lw);
+
+/* reset a list window (selected=0, start=0, clear=1) */
+void list_window_reset(list_window_t *lw);
+
+/* paint a list window */
+void list_window_paint(list_window_t *lw,
+                      list_window_callback_fn_t callback,
+                      void *callback_data);
+
+/* perform basic list window commands (movement) */
+int list_window_cmd(list_window_t *lw, int rows, command_t cmd);
+
+
+/* select functions */
+void list_window_set_selected(list_window_t *lw, int n);
+void list_window_previous(list_window_t *lw);
+void list_window_next(list_window_t *lw, int length);
+void list_window_first(list_window_t *lw);
+void list_window_last(list_window_t *lw, int length);
+void list_window_previous_page(list_window_t *lw);
+void list_window_next_page(list_window_t *lw, int length);
+
+/* find a string in a list window */
+int  list_window_find(list_window_t *lw, 
+                     list_window_callback_fn_t callback,
+                     void *callback_data,
+                     char *str);
+
+#endif
diff --git a/main.c b/main.c
index 4e420b75d4cc4c1be2ada8f86193092be908461e..c2c29b3aca577e651b5f04b53a97805b6555f4bd 100644 (file)
--- a/main.c
+++ b/main.c
@@ -121,7 +121,7 @@ main(int argc, const char *argv[])
            {
              screen_cmd(mpc, cmd);
              if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
-               counter=10;
+               counter=10; /* make shure we dont update the volume yet */
              else
                counter=0;
            }
index 42b8d9133281c884da5c0c26ea195c21b7d58cef..7b667d8238497f99097320cb41936ceba814bb6f 100644 (file)
--- a/screen.h
+++ b/screen.h
@@ -1,7 +1,7 @@
 #ifndef SCREEN_H
 #define SCREEN_H
 #include <ncurses.h>
-#include "screen_utils.h"
+#include "list_window.h"
 
 #define TOP_HEADER_PREFIX "Music Player Client - "
 #define TOP_HEADER_PLAY   TOP_HEADER_PREFIX "Playlist"
index ba0dbd3e1c948b280d70cbe2bd9b000085b05a1d..809ae3faf81f16f67319d0a6f243c66052180d26 100644 (file)
@@ -1,8 +1,3 @@
-/* 
- * $Id: screen_file.c,v 1.9 2004/03/18 09:33:07 kalle Exp $ 
- *
- */
-
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -14,6 +9,7 @@
 #include "mpc.h"
 #include "command.h"
 #include "screen.h"
+#include "screen_utils.h"
 #include "screen_file.h"
 
 #define BUFSIZE 1024
@@ -302,33 +298,11 @@ file_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
     {
     case CMD_PLAY:
       change_directory(screen, c);
-      break;
-    case CMD_LIST_PREVIOUS:
-      list_window_previous(screen->filelist);
-      screen->filelist->repaint=1;
-      break;
+      return 1;
     case CMD_SELECT:
       select_entry(screen, c);
       /* continue and select next item... */
-    case CMD_LIST_NEXT:
-      list_window_next(screen->filelist, c->filelist_length);
-      screen->filelist->repaint=1;
-      break;
-    case CMD_LIST_FIRST:
-      list_window_first(screen->filelist);
-      screen->filelist->repaint  = 1;
-      break;
-    case CMD_LIST_LAST:
-      list_window_last(screen->filelist, c->filelist_length);
-      screen->filelist->repaint  = 1;
-      break;
-    case CMD_LIST_NEXT_PAGE:
-      list_window_next_page(screen->filelist, c->filelist_length);
-      screen->filelist->repaint  = 1;
-      break;
-    case CMD_LIST_PREVIOUS_PAGE:
-      list_window_previous_page(screen->filelist);
-      screen->filelist->repaint  = 1;
+      cmd = CMD_LIST_NEXT;
       break;
     case CMD_LIST_FIND:
       if( screen->findbuf )
@@ -352,9 +326,9 @@ file_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
          screen_status_printf("Unable to find \'%s\'", screen->findbuf);
          beep();
        }
-      break;
+      return 1;
     default:
-      return 0;
+      break;
     }
-  return 1;
+  return list_window_cmd(screen->filelist, c->filelist_length, cmd);
 }
index 3d01363b91a1d552cf54e919c6e4eb67c1a03a2e..5320647be47712913f75dedb72aba36ceab974cd 100644 (file)
@@ -1,8 +1,3 @@
-/* 
- * $Id: screen_help.c,v 1.8 2004/03/17 13:40:25 kalle Exp $ 
- *
- */
-
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -13,6 +8,7 @@
 #include "mpc.h"
 #include "command.h"
 #include "screen.h"
+#include "screen_utils.h"
 #include "screen_help.h"
 
 typedef struct
@@ -137,30 +133,6 @@ help_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
 {
  switch(cmd)
     {
-    case CMD_LIST_PREVIOUS:
-      list_window_previous(screen->helplist);
-      screen->helplist->repaint=1;
-      break;
-    case CMD_LIST_NEXT:
-      list_window_next(screen->helplist, help_text_rows);
-      screen->helplist->repaint=1;
-      break;
-    case CMD_LIST_FIRST:
-      list_window_first(screen->helplist);
-      screen->helplist->repaint  = 1;
-      break;
-    case CMD_LIST_LAST:
-      list_window_last(screen->helplist, help_text_rows);
-      screen->helplist->repaint  = 1;
-      break;
-    case CMD_LIST_PREVIOUS_PAGE:
-      list_window_previous_page(screen->helplist);
-      screen->helplist->repaint  = 1;
-      break;
-    case CMD_LIST_NEXT_PAGE:
-      list_window_next_page(screen->helplist, help_text_rows);
-      screen->helplist->repaint  = 1;
-      break;
     case CMD_LIST_FIND:
       if( screen->findbuf )
        {
@@ -183,9 +155,9 @@ help_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
          screen_status_printf("Unable to find \'%s\'", screen->findbuf);
          beep();
        }
-      break;
+      return 1;
     default:
-      return 0;
+      break;
     }
-  return 1;
+  return list_window_cmd(screen->helplist, help_text_rows, cmd);
 }
index 9d1e9e80a42a4f2e9942cf41a3f5a303fb0360a3..48dc86961130fd940a3f0dab24869a6bb28d501f 100644 (file)
@@ -1,8 +1,3 @@
-/* 
- * $Id: screen_play.c,v 1.6 2004/03/17 14:49:31 kalle Exp $ 
- *
- */
-
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -12,6 +7,7 @@
 #include "mpc.h"
 #include "command.h"
 #include "screen.h"
+#include "screen_utils.h"
 #include "screen_file.h"
 #include "screen_play.h"
 
@@ -95,37 +91,14 @@ play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
       mpd_finishCommand(c->connection);
       screen_status_printf("Removed \'%s\' from playlist!",
                           mpc_get_song_name(song));
-      break;
-    case CMD_LIST_PREVIOUS:
-      list_window_previous(screen->playlist);
-      screen->playlist->repaint=1;
-      break;
-    case CMD_LIST_NEXT:
-      list_window_next(screen->playlist, c->playlist_length);
-      screen->playlist->repaint=1;
-      break;
-    case CMD_LIST_FIRST:
-      list_window_first(screen->playlist);
-      screen->playlist->repaint  = 1;
-      break;
-    case CMD_LIST_LAST:
-      list_window_last(screen->playlist, c->playlist_length);
-      screen->playlist->repaint  = 1;
-    case CMD_LIST_NEXT_PAGE:
-      list_window_next_page(screen->playlist, c->playlist_length);
-      screen->playlist->repaint  = 1;
-      break;
-    case CMD_LIST_PREVIOUS_PAGE:
-      list_window_previous_page(screen->playlist);
-      screen->playlist->repaint  = 1;
-      break;
+      return 1;
     case CMD_LIST_FIND:
       if( screen->findbuf )
        {
          free(screen->findbuf);
          screen->findbuf=NULL;
        }
-      /* fall throw... */
+      /* continue... */
     case CMD_LIST_FIND_NEXT:
       if( !screen->findbuf )
        screen->findbuf=screen_readln(screen->status_window.w, "/");
@@ -141,9 +114,9 @@ play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
          screen_status_printf("Unable to find \'%s\'", screen->findbuf);
          beep();
        }
-      break;
+      return 1;
     default:
-      return 0;
+      break;
     }
-  return 1;
+  return list_window_cmd(screen->playlist, c->playlist_length, cmd) ;
 }
index 8de23e16c6492d148d6d32b8b6dd7a9e51ec69a7..49bd6692a2682c3e14bd53940203af0c99cc3f93 100644 (file)
 #include "command.h"
 #include "screen.h"
 
-list_window_t *
-list_window_init(WINDOW *w, int width, int height)
-{
-  list_window_t *lw;
-
-  lw = malloc(sizeof(list_window_t));
-  memset(lw, 0, sizeof(list_window_t));
-  lw->w = w;
-  lw->cols = width;
-  lw->rows = height;
-  lw->clear = 1;
-  return lw;
-}
-
-list_window_t *
-list_window_free(list_window_t *lw)
-{
-  if( lw )
-    {
-      memset(lw, 0, sizeof(list_window_t));
-      free(lw);
-    }
-  return NULL;
-}
-
-void
-list_window_reset(list_window_t *lw)
-{
-  lw->selected = 0;
-  lw->start = 0;
-  lw->clear = 1;
-}
-
-void 
-list_window_set_selected(list_window_t *lw, int n)
-{
-  lw->selected=n;
-}
-
-void 
-list_window_paint(list_window_t *lw,
-                 list_window_callback_fn_t callback,
-                 void *callback_data)
-{
-  int i;
-
-  while( lw->selected < lw->start )
-    {
-      lw->start--;
-      lw->clear=1;
-    }
-  while( lw->selected >= lw->start+lw->rows )
-    {
-      lw->start++;
-      lw->clear=1;
-    }
-  if( lw->clear )
-    {
-      wclear(lw->w);
-      lw->clear=0;
-    }
-
-  for(i=0; i<lw->rows; i++)
-    {
-      int highlight;
-      char *label;
-
-      label = (callback) (lw->start+i, &highlight, callback_data);
-      if( label )
-       {
-         wmove(lw->w, i, 0);
-         if( highlight )
-           wattron(lw->w, A_BOLD);
-         if( lw->start+i == lw->selected )
-           wattron(lw->w, A_REVERSE);
-         
-         waddnstr(lw->w, label, lw->cols);
-
-         if( highlight )
-           wattroff(lw->w, A_BOLD);
-         if( lw->start+i == lw->selected )
-           wattroff(lw->w, A_REVERSE);
-       }
-    }
-}
-
-void
-list_window_next(list_window_t *lw, int length)
-{
-  if( lw->selected < length-1 )
-    lw->selected++;
-}
-
-void
-list_window_previous(list_window_t *lw)
-{
-  if( lw->selected > 0 )
-    lw->selected--;
-}
-
-void
-list_window_first(list_window_t *lw)
-{
-  lw->selected = 0;
-}
-
-void
-list_window_last(list_window_t *lw, int length)
-{
-  lw->selected = length-1;
-}
-
-void
-list_window_next_page(list_window_t *lw, int length)
-{
-  int step = lw->rows-1;
-  if( step<= 0 )
-    return;
-  if( lw->selected+step < length-1 )
-    lw->selected+=step;
-  else
-    return list_window_last(lw,length);
-}
-
-void
-list_window_previous_page(list_window_t *lw)
-{
-  int step = lw->rows-1;
-  if( step<= 0 )
-    return;
-  if( lw->selected-step > 0 )
-    lw->selected-=step;
-  else
-    list_window_first(lw);
-}
-
-int
-list_window_find(list_window_t *lw, 
-                list_window_callback_fn_t callback,
-                void *callback_data,
-                char *str)
-{
-  int h;
-  int i = lw->selected+1;
-  char *label;
-
-  while( (label=(callback) (i,&h,callback_data)) )
-    {
-      if( str && label && strcasestr(label, str) )
-       {
-         lw->selected = i;
-         return 0;
-       }
-      i++;
-    }
-  return 1;
-}
-
-
 char *
 screen_readln(WINDOW *w, char *prompt)
 {
index cddbe7e74f38d1595e23846ef2bbc5cc9fe4e601..70091f5c86a675b6ade53b7b071752cd86b28849 100644 (file)
@@ -1,48 +1,2 @@
 
-typedef char *  (*list_window_callback_fn_t)   (int index, 
-                                               int *highlight,
-                                               void *data);
-
-
-
-typedef struct
-{
-  WINDOW *w;
-  int rows, cols;
-
-  int start;
-  int selected;
-  int clear;
-  int repaint;
-
-} list_window_t;
-
-
-
-list_window_t *list_window_init(WINDOW *w, int width, int height);
-list_window_t *list_window_free(list_window_t *lw);
-
-
-void list_window_reset(list_window_t *lw);
-void list_window_set_selected(list_window_t *lw, int n);
-
-void list_window_paint(list_window_t *lw,
-                      list_window_callback_fn_t callback,
-                      void *callback_data);
-
-
-
-
-void list_window_previous(list_window_t *lw);
-void list_window_next(list_window_t *lw, int length);
-void list_window_first(list_window_t *lw);
-void list_window_last(list_window_t *lw, int length);
-void list_window_previous_page(list_window_t *lw);
-void list_window_next_page(list_window_t *lw, int length);
-int  list_window_find(list_window_t *lw, 
-                     list_window_callback_fn_t callback,
-                     void *callback_data,
-                     char *str);
-
-
 char *screen_readln(WINDOW *w, char *prompt);